Created
January 8, 2019 13:12
-
-
Save flutter-devs/be36131c0fe5a5bd9be6271bb1d07152 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Flutter | |
import UIKit | |
import Photos | |
public class SwiftFlutterGallaryPlugin: NSObject, FlutterPlugin { | |
public static func register(with registrar: FlutterPluginRegistrar) { | |
let channel = FlutterMethodChannel(name: "image_gallery", binaryMessenger: registrar.messenger()) | |
let instance = SwiftFlutterGallaryPlugin() | |
registrar.addMethodCallDelegate(instance, channel: channel) | |
} | |
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { | |
if (call.method == "getPlatformVersion") { | |
result("iOS " + UIDevice.current.systemVersion) | |
} | |
else if (call.method == "getAllImages") { | |
DispatchQueue.main.async { | |
let imgManager = PHImageManager.default() | |
let requestOptions = PHImageRequestOptions() | |
requestOptions.isSynchronous = true | |
let fetchOptions = PHFetchOptions() | |
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)] | |
let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions) | |
var allImages = [String]() | |
var totalItration = 0 | |
print("fetchResult.count : \(fetchResult.count)") | |
var savedLocalIdentifiers = [String]() | |
for index in 0..<fetchResult.count | |
{ | |
let asset = fetchResult.object(at: index) as PHAsset | |
let localIdentifier = asset.localIdentifier | |
savedLocalIdentifiers.append(localIdentifier) | |
imgManager.requestImage(for: asset, targetSize: CGSize(width: 512.0, height: 512.0), contentMode: PHImageContentMode.aspectFit, options: PHImageRequestOptions(), resultHandler:{(image, info) in | |
if image != nil { | |
var imageData: Data? | |
if let cgImage = image!.cgImage, cgImage.renderingIntent == .defaultIntent { | |
imageData = UIImageJPEGRepresentation(image!, 0.8) | |
} | |
else { | |
imageData = UIImagePNGRepresentation(image!) | |
} | |
let guid = ProcessInfo.processInfo.globallyUniqueString; | |
let tmpFile = String(format: "image_picker_%@.jpg", guid); | |
let tmpDirectory = NSTemporaryDirectory(); | |
let tmpPath = (tmpDirectory as NSString).appendingPathComponent(tmpFile); | |
if(FileManager.default.createFile(atPath: tmpPath, contents: imageData, attributes: [:])) { | |
allImages.append(tmpPath) | |
} | |
} | |
totalItration += 1 | |
if totalItration == (fetchResult.count) { | |
result(allImages) | |
} | |
}) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment