From ea46d46625594e81366d86056ace00f041684141 Mon Sep 17 00:00:00 2001 From: germancruzmoreno Date: Mon, 30 Mar 2020 17:34:48 -0600 Subject: [PATCH 1/3] IOS 13 get URL --- src/ios/PhotoLibraryService.swift | 44 +++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/ios/PhotoLibraryService.swift b/src/ios/PhotoLibraryService.swift index 456f94ab..882079bd 100644 --- a/src/ios/PhotoLibraryService.swift +++ b/src/ios/PhotoLibraryService.swift @@ -192,6 +192,14 @@ final class PhotoLibraryService { } return "application/octet-stream" } +public static func getFileURLFromPHAssetResourceDescription(description: String) -> String? { + let regex = try! NSRegularExpression(pattern: "(?<=fileURL: ).*(?=\\s)") + if let result = regex.firstMatch(in: description, options: [], range: NSRange(location: 0, length: description.count)) { + let url = String(description[Range(result.range, in: description)!]) + return url + } + return nil +} func getCompleteInfo(_ libraryItem: NSDictionary, completion: @escaping (_ fullPath: String?) -> Void) { @@ -211,18 +219,31 @@ final class PhotoLibraryService { fetchResult.enumerateObjects({ (obj: AnyObject, idx: Int, stop: UnsafeMutablePointer) -> Void in let asset = obj as! PHAsset - + let asset2 = obj as! PHAsset if(mediaType == "image") { + + self.imageRequestOptions.isNetworkAccessAllowed = true PHImageManager.default().requestImageData(for: asset, options: self.imageRequestOptions) { - (imageData: Data?, dataUTI: String?, orientation: UIImageOrientation, info: [AnyHashable: Any]?) in + (imageData: Data?, dataUTI: String?, orientation: UIImage.Orientation, info: [AnyHashable: Any]?) in if(imageData == nil) { completion(nil) } else { - let file_url:URL = info!["PHImageFileURLKey"] as! URL -// let mime_type = self.mimeTypes[file_url.pathExtension.lowercased()]! - completion(file_url.relativePath) + let version = OperatingSystemVersion(majorVersion: 13, minorVersion: 0, patchVersion: 0) + if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) { + let resourse = PHAssetResource.assetResources(for: asset) + let url = PhotoLibraryService.getFileURLFromPHAssetResourceDescription(description:resourse.description) + print("photoAsset.localIdentifier url", url) + + completion(url) + } else { + + let file_url:URL = info!["PHImageFileURLKey"] as! URL + completion(file_url.relativePath) + } + + } } } @@ -356,7 +377,7 @@ final class PhotoLibraryService { let asset = obj as! PHAsset PHImageManager.default().requestImageData(for: asset, options: self.imageRequestOptions) { - (imageData: Data?, dataUTI: String?, orientation: UIImageOrientation, info: [AnyHashable: Any]?) in + (imageData: Data?, dataUTI: String?, orientation: UIImage.Orientation, info: [AnyHashable: Any]?) in guard let image = imageData != nil ? UIImage(data: imageData!) : nil else { completion(nil) @@ -387,8 +408,9 @@ final class PhotoLibraryService { let mediaType = mimeType.components(separatedBy: "/")[0] if(mediaType == "image") { + PHImageManager.default().requestImageData(for: asset, options: self.imageRequestOptions) { - (imageData: Data?, dataUTI: String?, orientation: UIImageOrientation, info: [AnyHashable: Any]?) in + (imageData: Data?, dataUTI: String?, orientation: UIImage.Orientation, info: [AnyHashable: Any]?) in if(imageData == nil) { completion(nil) @@ -493,7 +515,7 @@ final class PhotoLibraryService { } // Permission was manually denied by user, open settings screen - let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) + let settingsUrl = URL(string: UIApplication.openSettingsURLString) if let url = settingsUrl { UIApplication.shared.openURL(url) // TODO: run callback only when return ? @@ -666,7 +688,7 @@ final class PhotoLibraryService { fileprivate func getDataFromURL(_ url: String) throws -> Data { if url.hasPrefix("data:") { - guard let match = self.dataURLPattern.firstMatch(in: url, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, url.characters.count)) else { // TODO: firstMatchInString seems to be slow for unknown reason + guard let match = self.dataURLPattern.firstMatch(in: url, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, url.count)) else { // TODO: firstMatchInString seems to be slow for unknown reason throw PhotoLibraryError.error(description: "The dataURL could not be parsed") } let dataPos = match.range(at: 0).length @@ -728,10 +750,10 @@ final class PhotoLibraryService { var mimeType: String? if (imageHasAlpha(image)){ - data = UIImagePNGRepresentation(image) + data = image.pngData() mimeType = data != nil ? "image/png" : nil } else { - data = UIImageJPEGRepresentation(image, CGFloat(quality)) + data = image.jpegData(compressionQuality: CGFloat(quality)) mimeType = data != nil ? "image/jpeg" : nil } From 7d768edd9faa1b824fb661c41866d748d960ad13 Mon Sep 17 00:00:00 2001 From: germancruzmoreno Date: Mon, 30 Mar 2020 17:36:02 -0600 Subject: [PATCH 2/3] Update PhotoLibrary.swift --- src/ios/PhotoLibrary.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/PhotoLibrary.swift b/src/ios/PhotoLibrary.swift index 04a6b5e4..702706bd 100644 --- a/src/ios/PhotoLibrary.swift +++ b/src/ios/PhotoLibrary.swift @@ -119,7 +119,7 @@ import Foundation let options = command.arguments[1] as! NSDictionary let thumbnailWidth = options["thumbnailWidth"] as! Int let thumbnailHeight = options["thumbnailHeight"] as! Int - let quality = options["quality"] as! Float + let quality = (options["quality"] as? NSNumber)?.floatValue ?? 1 service.getThumbnail(photoId, thumbnailWidth: thumbnailWidth, thumbnailHeight: thumbnailHeight, quality: quality) { (imageData) in From 276fd2e722cf8241c74876f0760059ba17f6033b Mon Sep 17 00:00:00 2001 From: germancruzmoreno Date: Mon, 30 Mar 2020 17:44:57 -0600 Subject: [PATCH 3/3] Update PhotoLibraryService.swift --- src/ios/PhotoLibraryService.swift | 40 ++++++++++++++----------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/ios/PhotoLibraryService.swift b/src/ios/PhotoLibraryService.swift index 882079bd..9195454a 100644 --- a/src/ios/PhotoLibraryService.swift +++ b/src/ios/PhotoLibraryService.swift @@ -192,14 +192,15 @@ final class PhotoLibraryService { } return "application/octet-stream" } -public static func getFileURLFromPHAssetResourceDescription(description: String) -> String? { - let regex = try! NSRegularExpression(pattern: "(?<=fileURL: ).*(?=\\s)") - if let result = regex.firstMatch(in: description, options: [], range: NSRange(location: 0, length: description.count)) { - let url = String(description[Range(result.range, in: description)!]) - return url + + public static func getFileURLFromPHAssetResourceDescription(description: String) -> String? { + let regex = try! NSRegularExpression(pattern: "(?<=fileURL: ).*(?=\\s)") + if let result = regex.firstMatch(in: description, options: [], range: NSRange(location: 0, length: description.count)) { + let url = String(description[Range(result.range, in: description)!]) + return url + } + return nil } - return nil -} func getCompleteInfo(_ libraryItem: NSDictionary, completion: @escaping (_ fullPath: String?) -> Void) { @@ -219,7 +220,7 @@ public static func getFileURLFromPHAssetResourceDescription(description: String) fetchResult.enumerateObjects({ (obj: AnyObject, idx: Int, stop: UnsafeMutablePointer) -> Void in let asset = obj as! PHAsset - let asset2 = obj as! PHAsset + if(mediaType == "image") { self.imageRequestOptions.isNetworkAccessAllowed = true @@ -230,20 +231,15 @@ public static func getFileURLFromPHAssetResourceDescription(description: String) completion(nil) } else { - let version = OperatingSystemVersion(majorVersion: 13, minorVersion: 0, patchVersion: 0) - if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) { - let resourse = PHAssetResource.assetResources(for: asset) - let url = PhotoLibraryService.getFileURLFromPHAssetResourceDescription(description:resourse.description) - print("photoAsset.localIdentifier url", url) - - completion(url) - } else { - - let file_url:URL = info!["PHImageFileURLKey"] as! URL - completion(file_url.relativePath) - } - - + let version = OperatingSystemVersion(majorVersion: 13, minorVersion: 0, patchVersion: 0) + if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) { + let resourse = PHAssetResource.assetResources(for: asset) + let url = PhotoLibraryService.getFileURLFromPHAssetResourceDescription(description:resourse.description) + completion(url) + } else { + let file_url:URL = info!["PHImageFileURLKey"] as! URL + completion(file_url.relativePath) + } } } }