Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ios 13 Fix #185

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ios/PhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 29 additions & 11 deletions src/ios/PhotoLibraryService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +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
}
return nil
}


func getCompleteInfo(_ libraryItem: NSDictionary, completion: @escaping (_ fullPath: String?) -> Void) {
Expand All @@ -211,18 +220,26 @@ final class PhotoLibraryService {
fetchResult.enumerateObjects({
(obj: AnyObject, idx: Int, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
let asset = 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)
completion(url)
} else {
let file_url:URL = info!["PHImageFileURLKey"] as! URL
completion(file_url.relativePath)
}
}
}
}
Expand Down Expand Up @@ -356,7 +373,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)
Expand Down Expand Up @@ -387,8 +404,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)
Expand Down Expand Up @@ -493,7 +511,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 ?
Expand Down Expand Up @@ -666,7 +684,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
Expand Down Expand Up @@ -728,10 +746,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
}

Expand Down