reformat iOS code
This commit is contained in:
parent
d21edf5cac
commit
7c48244058
@ -26,7 +26,14 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
let skip = arguments["skip"] as? NSNumber
|
let skip = arguments["skip"] as? NSNumber
|
||||||
let take = arguments["take"] as? NSNumber
|
let take = arguments["take"] as? NSNumber
|
||||||
let lightWeight = arguments["lightWeight"] as? Bool
|
let lightWeight = arguments["lightWeight"] as? Bool
|
||||||
result(listMedia(albumId: albumId, mediumType: mediumType, newest: newest, skip: skip, take: take, lightWeight: lightWeight))
|
result(listMedia(
|
||||||
|
albumId: albumId,
|
||||||
|
mediumType: mediumType,
|
||||||
|
newest: newest,
|
||||||
|
skip: skip,
|
||||||
|
take: take,
|
||||||
|
lightWeight: lightWeight
|
||||||
|
))
|
||||||
}
|
}
|
||||||
else if(call.method == "getMedium") {
|
else if(call.method == "getMedium") {
|
||||||
let arguments = call.arguments as! Dictionary<String, AnyObject>
|
let arguments = call.arguments as! Dictionary<String, AnyObject>
|
||||||
@ -105,7 +112,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var assetCollections : [PHAssetCollection] = []
|
private var assetCollections: [PHAssetCollection] = []
|
||||||
|
|
||||||
private func listAlbums(mediumType: String?, hideIfEmpty: Bool? = true) -> [[String: Any?]] {
|
private func listAlbums(mediumType: String?, hideIfEmpty: Bool? = true) -> [[String: Any?]] {
|
||||||
self.assetCollections = []
|
self.assetCollections = []
|
||||||
@ -147,14 +154,21 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
if let assetCollection = collection as? PHAssetCollection {
|
if let assetCollection = collection as? PHAssetCollection {
|
||||||
addCollection(collection: assetCollection, hideIfEmpty: hideIfEmpty)
|
addCollection(collection: assetCollection, hideIfEmpty: hideIfEmpty)
|
||||||
} else if let collectionList = collection as? PHCollectionList {
|
} else if let collectionList = collection as? PHCollectionList {
|
||||||
processPHCollections(fetchResult: PHCollectionList.fetchCollections(in: collectionList, options: nil), hideIfEmpty: hideIfEmpty)
|
processPHCollections(
|
||||||
|
fetchResult: PHCollectionList.fetchCollections(in: collectionList, options: nil),
|
||||||
|
hideIfEmpty: hideIfEmpty
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Smart Albums.
|
// Smart Albums.
|
||||||
processPHAssetCollections(
|
processPHAssetCollections(
|
||||||
fetchResult: PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .albumRegular, options: fetchOptions),
|
fetchResult: PHAssetCollection.fetchAssetCollections(
|
||||||
|
with: .smartAlbum,
|
||||||
|
subtype: .albumRegular,
|
||||||
|
options: fetchOptions
|
||||||
|
),
|
||||||
hideIfEmpty: hideIfEmpty ?? true
|
hideIfEmpty: hideIfEmpty ?? true
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -167,7 +181,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
albums.insert([
|
albums.insert([
|
||||||
"id": "__ALL__",
|
"id": "__ALL__",
|
||||||
"name": "All",
|
"name": "All",
|
||||||
"count" : countMedia(collection: nil, mediumType: mediumType),
|
"count": countMedia(collection: nil, mediumType: mediumType),
|
||||||
], at: 0)
|
], at: 0)
|
||||||
|
|
||||||
return albums
|
return albums
|
||||||
@ -183,7 +197,14 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
return PHAsset.fetchAssets(in: collection ?? PHAssetCollection.init(), options: options).count
|
return PHAsset.fetchAssets(in: collection ?? PHAssetCollection.init(), options: options).count
|
||||||
}
|
}
|
||||||
|
|
||||||
private func listMedia(albumId: String, mediumType: String?, newest: Bool, skip: NSNumber?, take: NSNumber?, lightWeight: Bool? = false) -> NSDictionary {
|
private func listMedia(
|
||||||
|
albumId: String,
|
||||||
|
mediumType: String?,
|
||||||
|
newest: Bool,
|
||||||
|
skip: NSNumber?,
|
||||||
|
take: NSNumber?,
|
||||||
|
lightWeight: Bool? = false
|
||||||
|
) -> NSDictionary {
|
||||||
let fetchOptions = PHFetchOptions()
|
let fetchOptions = PHFetchOptions()
|
||||||
fetchOptions.predicate = predicateFromMediumType(mediumType: mediumType)
|
fetchOptions.predicate = predicateFromMediumType(mediumType: mediumType)
|
||||||
fetchOptions.sortDescriptors = [
|
fetchOptions.sortDescriptors = [
|
||||||
@ -195,9 +216,15 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
collection.localIdentifier == albumId
|
collection.localIdentifier == albumId
|
||||||
})
|
})
|
||||||
|
|
||||||
let fetchResult = albumId == "__ALL__"
|
let fetchResult: PHFetchResult<PHAsset>
|
||||||
? PHAsset.fetchAssets(with: fetchOptions)
|
if(albumId == "__ALL__") {
|
||||||
: PHAsset.fetchAssets(in: collection ?? PHAssetCollection.init(), options: fetchOptions)
|
fetchResult = PHAsset.fetchAssets(with: fetchOptions)
|
||||||
|
} else {
|
||||||
|
fetchResult = PHAsset.fetchAssets(
|
||||||
|
in: collection ?? PHAssetCollection.init(),
|
||||||
|
options: fetchOptions
|
||||||
|
)
|
||||||
|
}
|
||||||
let start = skip?.intValue ?? 0
|
let start = skip?.intValue ?? 0
|
||||||
let total = fetchResult.count
|
let total = fetchResult.count
|
||||||
let end = take == nil ? total : min(start + take!.intValue, total)
|
let end = take == nil ? total : min(start + take!.intValue, total)
|
||||||
@ -258,12 +285,15 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
let imageSize = CGSize(width: width?.intValue ?? 128, height: height?.intValue ?? 128)
|
let imageSize = CGSize(width: width?.intValue ?? 128, height: height?.intValue ?? 128)
|
||||||
manager.requestImage(
|
manager.requestImage(
|
||||||
for: asset,
|
for: asset,
|
||||||
targetSize: CGSize(width: imageSize.width * UIScreen.main.scale, height: imageSize.height * UIScreen.main.scale),
|
targetSize: CGSize(
|
||||||
|
width: imageSize.width * UIScreen.main.scale,
|
||||||
|
height: imageSize.height * UIScreen.main.scale
|
||||||
|
),
|
||||||
contentMode: PHImageContentMode.aspectFill,
|
contentMode: PHImageContentMode.aspectFill,
|
||||||
options: options,
|
options: options,
|
||||||
resultHandler: {(uiImage: UIImage?, info) in
|
resultHandler: { (uiImage: UIImage?, info) in
|
||||||
guard let image = uiImage else {
|
guard let image = uiImage else {
|
||||||
completion(nil , NSError(domain: "photo_gallery", code: 404, userInfo: nil))
|
completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let bytes = image.jpegData(compressionQuality: CGFloat(70))
|
let bytes = image.jpegData(compressionQuality: CGFloat(70))
|
||||||
@ -273,7 +303,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
completion(nil , NSError(domain: "photo_gallery", code: 404, userInfo: nil))
|
completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
private func getAlbumThumbnail(
|
private func getAlbumThumbnail(
|
||||||
@ -296,11 +326,14 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
fetchOptions.fetchLimit = 1
|
fetchOptions.fetchLimit = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
let assets = albumId == "__ALL__" ?
|
let assets: PHFetchResult<PHAsset>
|
||||||
PHAsset.fetchAssets(with: fetchOptions) :
|
if(albumId == "__ALL__") {
|
||||||
PHAsset.fetchAssets(in: self.assetCollections.first(where: { (collection) -> Bool in
|
assets = PHAsset.fetchAssets(with: fetchOptions)
|
||||||
|
} else {
|
||||||
|
assets = PHAsset.fetchAssets(in: self.assetCollections.first(where: { (collection) -> Bool in
|
||||||
collection.localIdentifier == albumId
|
collection.localIdentifier == albumId
|
||||||
})!, options: fetchOptions)
|
})!, options: fetchOptions)
|
||||||
|
}
|
||||||
|
|
||||||
if (assets.count > 0) {
|
if (assets.count > 0) {
|
||||||
let asset: PHAsset = assets[0]
|
let asset: PHAsset = assets[0]
|
||||||
@ -320,9 +353,9 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
),
|
),
|
||||||
contentMode: PHImageContentMode.aspectFill,
|
contentMode: PHImageContentMode.aspectFill,
|
||||||
options: options,
|
options: options,
|
||||||
resultHandler: {(uiImage: UIImage?, info) in
|
resultHandler: { (uiImage: UIImage?, info) in
|
||||||
guard let image = uiImage else {
|
guard let image = uiImage else {
|
||||||
completion(nil , NSError(domain: "photo_gallery", code: 404, userInfo: nil))
|
completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let bytes = image.jpegData(compressionQuality: CGFloat(80))
|
let bytes = image.jpegData(compressionQuality: CGFloat(80))
|
||||||
@ -332,7 +365,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
completion(nil , NSError(domain: "photo_gallery", code: 404, userInfo: nil))
|
completion(nil, NSError(domain: "photo_gallery", code: 404, userInfo: nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
private func getFile(mediumId: String, mimeType: String?, completion: @escaping (String?, Error?) -> Void) {
|
private func getFile(mediumId: String, mimeType: String?, completion: @escaping (String?, Error?) -> Void) {
|
||||||
@ -381,8 +414,7 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} else if(asset.mediaType == PHAssetMediaType.video
|
} else if(asset.mediaType == PHAssetMediaType.video || asset.mediaType == PHAssetMediaType.audio) {
|
||||||
|| asset.mediaType == PHAssetMediaType.audio) {
|
|
||||||
let options = PHVideoRequestOptions()
|
let options = PHVideoRequestOptions()
|
||||||
options.version = .current
|
options.version = .current
|
||||||
options.deliveryMode = .highQualityFormat
|
options.deliveryMode = .highQualityFormat
|
||||||
@ -526,7 +558,10 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
guard let assetUTI = uti else {
|
guard let assetUTI = uti else {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
guard let ext = UTTypeCopyPreferredTagWithClass(assetUTI as CFString, kUTTagClassFilenameExtension as CFString)?.takeRetainedValue() as String? else {
|
guard let ext = UTTypeCopyPreferredTagWithClass(
|
||||||
|
assetUTI as CFString,
|
||||||
|
kUTTagClassFilenameExtension as CFString
|
||||||
|
)?.takeRetainedValue() as String? else {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return "." + ext
|
return "." + ext
|
||||||
@ -536,7 +571,10 @@ public class SwiftPhotoGalleryPlugin: NSObject, FlutterPlugin {
|
|||||||
guard let assetUTI = uti else {
|
guard let assetUTI = uti else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
guard let mimeType = UTTypeCopyPreferredTagWithClass(assetUTI as CFString, kUTTagClassMIMEType as CFString)?.takeRetainedValue() as String? else {
|
guard let mimeType = UTTypeCopyPreferredTagWithClass(
|
||||||
|
assetUTI as CFString,
|
||||||
|
kUTTagClassMIMEType as CFString
|
||||||
|
)?.takeRetainedValue() as String? else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return mimeType
|
return mimeType
|
||||||
|
Loading…
x
Reference in New Issue
Block a user