update getting medium info from asset on iOS platform

This commit is contained in:
Wenqi Li 2023-07-27 00:26:16 +08:00
parent 977c2ca2c6
commit 9f8e0aefca

View File

@ -228,12 +228,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil)) completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
} else { } else {
let asset: PHAsset = assets[0] let asset: PHAsset = assets[0]
getMediumFromAssetAsync( completion(getMediumFromAsset(asset: asset), nil)
asset: asset,
completion: { (data: [String: Any?]?, error: Error?) -> Void in
completion(data, nil)
}
)
} }
} }
@ -436,6 +431,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
let mimeType = self.extractMimeTypeFromAsset(asset: asset) let mimeType = self.extractMimeTypeFromAsset(asset: asset)
let resource = self.extractResourceFromAsset(asset: asset) let resource = self.extractResourceFromAsset(asset: asset)
let size = self.extractSizeFromResource(resource: resource) let size = self.extractSizeFromResource(resource: resource)
let orientation = self.toOrientationValue(orientation: asset.value(forKey: "orientation") as? UIImage.Orientation)
return [ return [
"id": asset.localIdentifier, "id": asset.localIdentifier,
"filename": filename, "filename": filename,
@ -445,6 +441,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
"height": asset.pixelHeight, "height": asset.pixelHeight,
"width": asset.pixelWidth, "width": asset.pixelWidth,
"size": size, "size": size,
"orientation": orientation,
"duration": NSInteger(asset.duration * 1000), "duration": NSInteger(asset.duration * 1000),
"creationDate": (asset.creationDate != nil) ? NSInteger(asset.creationDate!.timeIntervalSince1970 * 1000) : nil, "creationDate": (asset.creationDate != nil) ? NSInteger(asset.creationDate!.timeIntervalSince1970 * 1000) : nil,
"modifiedDate": (asset.modificationDate != nil) ? NSInteger(asset.modificationDate!.timeIntervalSince1970 * 1000) : nil "modifiedDate": (asset.modificationDate != nil) ? NSInteger(asset.modificationDate!.timeIntervalSince1970 * 1000) : nil
@ -463,34 +460,6 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
] ]
} }
private func getMediumFromAssetAsync(asset: PHAsset, completion: @escaping ([String : Any?]?, Error?) -> Void) -> Void {
let filename = self.extractFilenameFromAsset(asset: asset)
let mimeType = self.extractMimeTypeFromAsset(asset: asset)
let resource = self.extractResourceFromAsset(asset: asset)
let size = self.extractSizeFromResource(resource: resource)
let manager = PHImageManager.default()
manager.requestImageData(
for: asset,
options: nil,
resultHandler: { (data: Data?, uti: String?, orientation: UIImage.Orientation, info: ([AnyHashable: Any]?)) -> Void in
completion([
"id": asset.localIdentifier,
"filename": filename,
"title": self.extractTitleFromFilename(filename: filename),
"mediumType": self.toDartMediumType(value: asset.mediaType),
"mimeType": mimeType,
"height": asset.pixelHeight,
"width": asset.pixelWidth,
"size": size,
"orientation": self.toOrientationValue(orientation: orientation),
"duration": NSInteger(asset.duration * 1000),
"creationDate": (asset.creationDate != nil) ? NSInteger(asset.creationDate!.timeIntervalSince1970 * 1000) : nil,
"modifiedDate": (asset.modificationDate != nil) ? NSInteger(asset.modificationDate!.timeIntervalSince1970 * 1000) : nil
], nil)
}
)
}
private func exportPathForAsset(asset: PHAsset, ext: String) -> URL { private func exportPathForAsset(asset: PHAsset, ext: String) -> URL {
let mediumId = asset.localIdentifier let mediumId = asset.localIdentifier
.replacingOccurrences(of: "/", with: "__") .replacingOccurrences(of: "/", with: "__")
@ -517,7 +486,10 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
} }
} }
private func toOrientationValue(orientation: UIImage.Orientation) -> Int { private func toOrientationValue(orientation: UIImage.Orientation?) -> Int {
guard let orientation = orientation else {
return 0
}
switch orientation { switch orientation {
case UIImage.Orientation.up: case UIImage.Orientation.up:
return 1 return 1