Closed ambr89 closed 5 years ago
It is not advised to get the URL of the PHAsset
. Instead, you can request a UIImage version or a plain data version using PHImageManager
(yes you also use PHImageManager
for video).
This also allows you to get a cropped or resized version of the image and it handles iCloud downloading for you.
If you really do want to use (which again, is not advised) use can use the PHAssetResource
for a PHAsset
which will return a single or multiple resources based on the PHAsset
Also as a side note: PHImageManager
will give an unclear error when a PHAsset's photo needs to be downloaded from iCloud AND low power mode is enabled. To get around this I've added a custom error message in the app telling the user that low power mode should be turned off to download the photo.
You can check for low power mode with the following code:
let lowerPowerModeEnabled = ProcessInfo.processInfo.isLowPowerModeEnabled
Ok, thanks I'm trying to use PHAssetResource, and obtain from this the URL. After this, I need to show the result in a CollectionView and send all to server.
Thanks a lot! I've resolved!
func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset]) { pickerViewController.dismiss(animated:true, completion: nil) for ass in assets{ let res2:[PHAssetResource] = PHAssetResource.assetResources(for: ass) let obj = res2[0] as NSObject let strURL = obj.value(forKey: "_fileURL") as! NSURL let data = try? Data(contentsOf: strURL as URL) let img = UIImage(data: data!) } }
This image getting URL:
Assets [<PHAsset: 0x11dc12570> 2E25AD2C-51D8-4431-882F-AC8CA2E8F65B/L0/001 mediaType=1/0, sourceType=1, (3024x4032), creationDate=2019-04-08 10:04:57 +0000, location=0, hidden=0, favorite=0 ]
This image not getting URL: (app will crash for data nil)
Assets [<PHAsset: 0x11dc065f0> 420A3B33-A758-4EC3-9DC1-D72AC88B98C9/L0/001 mediaType=1/4, sourceType=1, (1242x2208), creationDate=2019-04-11 07:14:37 +0000, location=0, hidden=0, favorite=0 ]
what are the difference b/w the above two Assets, How to resolve this issue ?
Hi,
I've try to read property of PHAsset, but I haven't understand how I can get information about image selected.
How can get NSURL or UIImage?