iOS 13 beta4 no longer gives
1) PHImageFileURLKey 2) PHImageResultIsDegradedKey in image result info keys.
Anyone knows a way to find the fileurl of the P
In iOS 13.0, PHImageFileURLKey is removed. In order to access PHAsset data, When we call this function to requestImageData we can get image information through this key PHImageFileUTIKey.
PHImageManager.default().requestImageData(for: asset, options: PHImageRequestOptions(), resultHandler:{ [weak self] (imagedata, dataUTI, orientation, info) in
if let assetInfo = info, let fileURLKey = assetInfo["PHImageFileUTIKey"] as? NSString {
let fileComp = fileURLKey.components(separatedBy: ".")
if fileComp.count > 0 {
// Do your stuff.
}
})
1.PHImageResultIsDegradedKey is now being available in latest iOS 13 GM and GM2. So one problem solved.
2.PHImageFileURLKey won't be available in iOS 13 and if you are in so need of fileURL of an image asset and you need it without any PhotoKit's methods callback go for regex in description of asset. It will work.
You can get the description
of a PHAssetResource object using +[PHAssetResource assetResourcesForAsset:] , use first object from resulting array and then extract the fileURL
from string using regex:
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
}
Note: Only available from iOS9
let resourse = PHAssetResource.assetResources(for: self.phasset)
let url = resourse.first?.originalFilename
This works for me !
In case someone is looking to achieve the same on Xamarin.iOS, this is my C# version of the swift code:
phAsset.RequestContentEditingInput(new PHContentEditingInputRequestOptions(), phcontentediting);
and then:
private void phcontentediting(PHContentEditingInput contentEditingInput, NSDictionary requestStatusInfo)
{
}
when phcontentediting
is called, contentEditingInput has a lot of useful information such as CreationDate
, FullSizeImageUrl
and even Location
!
If anyone needs the file size when using RequestContentEditingInput in Xamarin.
var options = new PHContentEditingInputRequestOptions();
asset.RequestContentEditingInput(options, (PHContentEditingInput contentEditingInput, NSDictionary requestStatusInfo) =>
{
var imageUrl = contentEditingInput.FullSizeImageUrl.ToString();
NSObject fileSizeObj;
if (contentEditingInput.FullSizeImageUrl.TryGetResource(new NSString("NSURLFileSizeKey"), out fileSizeObj))
{
var fileSizeNSNum = fileSizeObj as NSNumber;
long fileSize = fileSizeNSNum.Int64Value;
}
// Make sure to dispose or your app will crash with a lot of photos!
contentEditingInput.Dispose();
});
You can use
let photoPath = photoAsset.localIdentifier
to replace the
let photoPath = info["PHImageFileURLKey"]