index.uts 9.1 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1
import {
DCloud-yyl's avatar
DCloud-yyl 已提交
2 3
	ChooseImageOptions, ChooseImage,
	PreviewImageOptions, PreviewImage,
DCloud-yyl's avatar
DCloud-yyl 已提交
4
	ClosePreviewImage, ClosePreviewImageSuccess, ClosePreviewImageOptions,
DCloud-yyl's avatar
DCloud-yyl 已提交
5 6 7 8 9 10 11
	SaveImageToPhotosAlbum, SaveImageToPhotosAlbumOptions, SaveImageToPhotosAlbumSuccess,
	GetImageInfo, GetImageInfoOptions, GetImageInfoSuccess,
	CompressImage, CompressImageOptions,
	ChooseVideo, ChooseVideoOptions,
	SaveVideoToPhotosAlbum, SaveVideoToPhotosAlbumOptions, SaveVideoToPhotosAlbumSuccess,
	GetVideoInfo, GetVideoInfoOptions,
	CompressVideo, CompressVideoOptions
DCloud-yyl's avatar
DCloud-yyl 已提交
12 13
} from "../interface.uts";
import {
DCloud-yyl's avatar
DCloud-yyl 已提交
14 15
	UniError_ChooseImage, UniError_SaveImageToPhotosAlbum, UniError_GetImageInfo, UniError_CompressImage,
	MediaErrorImpl, UniError_PreviewImage, UniError_ChooseVideo, UniError_SaveVideoToPhotosAlbum, UniError_GetVideoInfo, UniError_CompressVideo
DCloud-yyl's avatar
DCloud-yyl 已提交
16
} from "../unierror.uts"
DCloud-yyl's avatar
DCloud-yyl 已提交
17 18 19 20
import { uniChooseImage, uniChooseVideo } from "./utils/chooseUtils.uts";
import { requestAlbumPermission, requestCameraPermission, requestMicrophonePermission } from "./utils/requestPermission.uts";
import { isNetUrl, getImageOrientation, getImageFormat, saveImage } from "./utils/handleImageUtils.uts";
import { DCUniMediaPicker } from "./utils/mediaPicker.uts";
DCloud-yyl's avatar
DCloud-yyl 已提交
21
import { UTSiOS } from "DCloudUTSFoundation";
DCloud-yyl's avatar
DCloud-yyl 已提交
22 23 24
import { URL, Data, FileAttributeKey } from 'Foundation';
import { UIImage } from 'UIKit';
import { AVURLAsset, AVMediaType } from 'AVFoundation';
DCloud-yyl's avatar
DCloud-yyl 已提交
25 26 27 28 29 30 31 32

let mediaPicker : DCUniMediaPicker = new DCUniMediaPicker();

export const chooseImage : ChooseImage = function (option : ChooseImageOptions) {
	uniChooseImage(option, function (count : number, compressed : boolean, index : number) {
		if (index == 0) {
			requestCameraPermission(function (status : number) {
				if (status == 1) {
DCloud-yyl's avatar
DCloud-yyl 已提交
33
					mediaPicker.chooseImageWithCamera(option)
DCloud-yyl's avatar
DCloud-yyl 已提交
34 35 36 37 38 39 40 41 42 43
				} else {
					let error = new MediaErrorImpl(1101005, UniError_ChooseImage);
					option.fail?.(error)
					option.complete?.(error)
				}
			})

		} else if (index == 1) {
			requestAlbumPermission("readWrite", function (status : number) {
				if (status == 1) {
DCloud-yyl's avatar
DCloud-yyl 已提交
44
					mediaPicker.chooseImageWithAlbum(option, count)
DCloud-yyl's avatar
DCloud-yyl 已提交
45 46 47 48 49 50 51 52 53 54
				} else {
					let error = new MediaErrorImpl(1101005, UniError_ChooseImage);
					option.fail?.(error)
					option.complete?.(error)
				}
			})
		}
	});
}

DCloud-yyl's avatar
DCloud-yyl 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
export const chooseVideo : ChooseVideo = function (option : ChooseVideoOptions) {
	uniChooseVideo(option, (count : number, compressed : boolean, index : number) => {
		if (index == 0) {
			requestCameraPermission(function (status : number) {
				if (status == 1) {
					requestMicrophonePermission(function (status : number) {
						if (status == 1) {
							mediaPicker.chooseVideoWithCamera(option)
						} else {
							let error = new MediaErrorImpl(1101005, UniError_ChooseVideo);
							option.fail?.(error)
							option.complete?.(error)
						}
					})
DCloud-yyl's avatar
DCloud-yyl 已提交
69 70

				} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
71 72 73
					let error = new MediaErrorImpl(1101005, UniError_ChooseVideo);
					option.fail?.(error)
					option.complete?.(error)
DCloud-yyl's avatar
DCloud-yyl 已提交
74 75
				}
			})
DCloud-yyl's avatar
DCloud-yyl 已提交
76 77 78 79 80

		} else if (index == 1) {
			requestAlbumPermission("readWrite", function (status : number) {
				if (status == 1) {
					mediaPicker.chooseVideoWithAlbum(option)
DCloud-yyl's avatar
DCloud-yyl 已提交
81
				} else {
DCloud-yyl's avatar
DCloud-yyl 已提交
82 83 84
					let error = new MediaErrorImpl(1101005, UniError_ChooseVideo);
					option.fail?.(error)
					option.complete?.(error)
DCloud-yyl's avatar
DCloud-yyl 已提交
85 86 87
				}
			})
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
88
	})
DCloud-yyl's avatar
DCloud-yyl 已提交
89 90 91
}

export const previewImage : PreviewImage = function (options : PreviewImageOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
92 93 94 95 96 97
	if (options.urls.length > 0) {
		mediaPicker.preview(options);
	} else {
		let error = new MediaErrorImpl(1101002, UniError_PreviewImage);
		options.fail?.(error)
		options.complete?.(error)
DCloud-yyl's avatar
DCloud-yyl 已提交
98
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
99
}
DCloud-yyl's avatar
DCloud-yyl 已提交
100

DCloud-yyl's avatar
DCloud-yyl 已提交
101 102 103 104 105 106
export const closePreviewImage : ClosePreviewImage = function (options : ClosePreviewImageOptions) {
	mediaPicker.closePreview();
	let callback : ClosePreviewImageSuccess = {
		errMsg: "ok"
	}
	options.success?.(callback)
DCloud-yyl's avatar
DCloud-yyl 已提交
107 108 109 110
	options.complete?.(callback)
}

export const saveImageToPhotosAlbum : SaveImageToPhotosAlbum = function (options : SaveImageToPhotosAlbumOptions) {
DCloud-yyl's avatar
DCloud-yyl 已提交
111
	const path = UTSiOS.getResourceAbsolutePath(options.filePath, null)
DCloud-yyl's avatar
DCloud-yyl 已提交
112 113 114 115 116 117 118 119
	let url = new URL(string = path)
	if (url == null) {
		let error = new MediaErrorImpl(1101003, UniError_SaveImageToPhotosAlbum);
		options.fail?.(error)
		options.complete?.(error)
		return
	}
	requestAlbumPermission("addOnly", function (status : number) {
DCloud-yyl's avatar
DCloud-yyl 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133
		if (status == 1) {
			try {
				UTSiOS.try(PHPhotoLibrary.shared().performChangesAndWait(() : void => {
					PHAssetCreationRequest.creationRequestForAssetFromImage(atFileURL = url!)
				}))
				let success : SaveImageToPhotosAlbumSuccess = {
					"path": path
				}
				options.success?.(success)
				options.complete?.(success)
			} catch (e) {
				let error = new MediaErrorImpl(1101006, UniError_SaveImageToPhotosAlbum);
				options.fail?.(error)
				options.complete?.(error)
DCloud-yyl's avatar
DCloud-yyl 已提交
134 135 136 137 138 139 140
			}
		} else {
			let error = new MediaErrorImpl(1101005, UniError_SaveImageToPhotosAlbum);
			options.fail?.(error)
			options.complete?.(error)
		}
	})
DCloud-yyl's avatar
DCloud-yyl 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
}

export const saveVideoToPhotosAlbum : SaveVideoToPhotosAlbum = function (options : SaveVideoToPhotosAlbumOptions) {
	const path = UTSiOS.getResourceAbsolutePath(options.filePath, null)
	let url = new URL(string = path)
	if (url == null) {
		let error = new MediaErrorImpl(1101003, UniError_SaveVideoToPhotosAlbum);
		options.fail?.(error)
		options.complete?.(error)
		return
	}
	requestAlbumPermission("addOnly", function (status : number) {
		if (status == 1) {
			try {
				UTSiOS.try(PHPhotoLibrary.shared().performChangesAndWait(() : void => {
					PHAssetCreationRequest.creationRequestForAssetFromVideo(atFileURL = url!)
				}))
				let success : SaveVideoToPhotosAlbumSuccess = {}
				options.success?.(success)
				options.complete?.(success)
			} catch (e) {
				let error = new MediaErrorImpl(1101006, UniError_SaveVideoToPhotosAlbum);
				options.fail?.(error)
				options.complete?.(error)
			}
		} else {
			let error = new MediaErrorImpl(1101005, UniError_SaveVideoToPhotosAlbum);
			options.fail?.(error)
			options.complete?.(error)
		}
	})
}

export const getImageInfo : GetImageInfo = function (options : GetImageInfoOptions) {
	if (options.src.length > 0) {
		if (isNetUrl(options.src)) {
			UTSiOS.loadImage(options.src, (image, data) => {
DCloud-yyl's avatar
DCloud-yyl 已提交
178 179 180 181 182 183 184 185 186
				if (image != null) {
					let imageData : Data | null = null;
					if(data != null){
						imageData = data;
					}else{
						imageData = image!.jpegData(compressionQuality= 1.0);
					}
					const type = getImageFormat(imageData!);
					const path = saveImage(imageData!, type);
DCloud-yyl's avatar
DCloud-yyl 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
					if (path.count > 0) {
						DispatchQueue.main.async(execute = () : void => {
							let successCallback : GetImageInfoSuccess = {
								"width": image!.size.width,
								"height": image!.size.height,
								"path": "file://" + path,
								"orientation": getImageOrientation(image!),
								"type": type
							}
							options.success?.(successCallback)
							options.complete?.(successCallback)
						})
						return;
					}
				}
				DispatchQueue.main.async(execute = () : void => {
					let failCallback = new MediaErrorImpl(1101004, UniError_GetImageInfo);
					options.fail?.(failCallback)
					options.complete?.(failCallback)
				})
			});
		} else {
			const path = UTSiOS.getResourceAbsolutePath(options.src, null)
			const url = new URL(fileURLWithPath = path);
			let imageData : Data | null = null;
			try {
				imageData = UTSiOS.try(Data(contentsOf= url))
			} catch (e) {
				// console.log(e)
			}

			if (imageData != null) {
				const image = new UIImage(data = imageData!);
				if (image != null) {
					let successCallback : GetImageInfoSuccess = {
						"width": image!.size.width,
						"height": image!.size.height,
						"path": "file://" + path,
						"orientation": getImageOrientation(image!),
						"type": getImageFormat(imageData!)
					}
					options.success?.(successCallback)
					options.complete?.(successCallback)
					return;
				}
			}
			let failCallback = new MediaErrorImpl(1101004, UniError_GetImageInfo);
			options.fail?.(failCallback)
			options.complete?.(failCallback)
		}
	} else {
		let failCallback = new MediaErrorImpl(1101003, UniError_GetImageInfo);
		options.fail?.(failCallback)
		options.complete?.(failCallback)
	}
}

export const getVideoInfo : GetVideoInfo = function (options : GetVideoInfoOptions) {
	if (options.src.length > 0) {
		mediaPicker.getVideoInfo(options);
	} else {
		let error = new MediaErrorImpl(1101003, UniError_GetVideoInfo);
		options.fail?.(error)
		options.complete?.(error)
	}
}

export const compressImage : CompressImage = function (options : CompressImageOptions) {
	if (options.src.length > 0) {
		mediaPicker.compressImage(options);
	} else {
		let error = new MediaErrorImpl(1101003, UniError_CompressImage);
		options.fail?.(error)
		options.complete?.(error)
	}
}

export const compressVideo : CompressVideo = function (options : CompressVideoOptions) {
	if (options.src.length > 0) {
		mediaPicker.compressVideo(options);
	} else {
		let error = new MediaErrorImpl(1101003, UniError_CompressVideo);
		options.fail?.(error)
		options.complete?.(error)
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
272
}