UIImagePickerControllerReferenceURL was deprecated in iOS 11 (even though it's still returned in the info dictionary) and was supposed to be replaced with UIImagePickerControllerPHAsset, but I've yet to get an info dictionary back which contains that key. Since the one is deprecated and the other is missing, is there a known solution for extracting the "date taken" from the picked image?
For reference, this is an example info dictionary returned when and image is picked from the library:
▿ 4 elements
▿ 0 : 2 elements
- key : "UIImagePickerControllerImageURL"
- value : file:///private/var/mobile/Containers/Data/Application/EE1BA60E-2DC3-47C5-A58D-86498E95C323/tmp/3A025D4C-B378-474B-8A09-017479A3A776.jpeg
▿ 1 : 2 elements
- key : "UIImagePickerControllerMediaType"
- value : public.image
▿ 2 : 2 elements
- key : "UIImagePickerControllerReferenceURL"
- value : assets-library://asset/asset.HEIC?id=537976CD-A550-41C9-9416-92C8072112D7&ext=HEIC
▿ 3 : 2 elements
- key : "UIImagePickerControllerOriginalImage"
- value : <UIImage: 0x1d04b4760> size {3024, 4032} orientation 3 scale 1.000000
(Note that UIImagePickerControllerReferenceURL is still present, though deprecated, and the suggested replacement, UIImagePickerControllerPHAsset, is missing.)
If it were present, getting the date would be simple:
if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset,
let resource = PHAssetResource.assetResources(for: asset).first {
let dateTaken = resource.creationDate
}
Could it be that Apple forgot to implement UIImagePickerControllerPHAsset? Any ideas on workarounds (without using deprecated methods)?
Note on possible duplicates
I believe that previous solutions on Stack Overflow are deprecated, and thus won't answer the question using modern approaches.
Swift 4.1
I struggled with this for a while too. It turns out you just need user permission to access to the photo library - then info will contain a value for the key UIImagePickerControllerPHAsset. You can check & request access like this:
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
// show your media picker
case .denied:
// probably alert the user that they need to grant photo access
case .notDetermined:
PHPhotoLibrary.requestAuthorization({status in
if status == .authorized {
// show your media picker
}
})
case .restricted:
// probably alert the user that photo access is restricted
}
Then you use the imagePickerController(_: didFinishPickingMediaWithInfo:) method as usual and will have access to the UIImagePickerControllerPHAsset
You can get the date by examining the chosen photo's metadata through the ImageIO framework.
However, the claim that the PHAsset information doesn't arrive is simply bogus. It arrives just fine. You didn't show your code, so who knows what you're doing? Perhaps the problem is that you forgot to get user authorization? Without user authorization, of course you can't access the PHAsset. You'll get the UIImagePickerControllerOriginalImage and the UIImagePickerControllerImageURL and that's all.
This code works just fine for me (assuming we have the necessary user authorization before using the image picker controller in the first place):
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) { //
let asset = info[UIImagePickerControllerPHAsset] as? PHAsset
let url = info[UIImagePickerControllerMediaURL] as? URL
var im = info[UIImagePickerControllerOriginalImage] as? UIImage
if let ed = info[UIImagePickerControllerEditedImage] as? UIImage {
im = ed
}
let live = info[UIImagePickerControllerLivePhoto] as? PHLivePhoto
let imurl = info[UIImagePickerControllerImageURL] as? URL
self.dismiss(animated:true) {
if let style = asset?.playbackStyle { // and so on, works fine
// ....
}
}
}
Related
I need to get the metaData from an Image I'm picking via UIImagePickerController.
This is my code:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
let metaData = info[UIImagePickerController.InfoKey.mediaMetadata] as? [AnyHashable: Any]
print(metaData)
picker.dismiss(animated: true, completion: nil)
}
It works fine, when im picking the Image having .camera as source. But when I use .photoLibrary as source, then metaData is nil. I already read through other questions and tried stuff like
let asset = info[.phAsset] as? PHAsset
print(asset?.creationDate ?? "None")
print(asset?.location ?? "None")
But this also returns nil. I guess the source of the problem is, that the info-Dictionary only returns 4 Keys when picking from .photoLibrary:
UIImagePickerControllerOriginalImage
UIImagePickerControllerMediaType
UIImagePickerControllerImageURL
UIImagePickerControllerReferenceURL
It would be awesome if someone could tell me where my mistake is.
Thanks in advance !
I had the same problem. If the user does not grant access to the photo library first, info[.phAsset] will return nil.
Quote from Apple: "Accessing the photo library always requires explicit permission from the user. The first time your app uses PHAsset, PHCollection, PHAssetCollection, or PHCollectionList methods to fetch content from the library ..."
Thus, you have to call PHPhotoLibrary.requestAuthorization{ ... } before presenting the image picker.
If the user denies the request, info[.phAsset] will also be nil!
This key is valid only when using an image picker whose source type is set to UIImagePickerController.SourceType.camera, and applies only to still images.
The value for this key is an NSDictionary object that contains the metadata of the photo that was just captured. To store the metadata along with the image in the Camera Roll, use the PHAssetChangeRequest class from the Photos framework.
https://developer.apple.com/documentation/uikit/uiimagepickercontroller/infokey/1619147-mediametadata
im trying to do the follow : user is picking image from photo lib as profile pic -> from now on this image will use as profile pic even if the app closed .
im using this code :
if let imageUrl = info[UIImagePickerController.InfoKey.referenceURL] as? NSURL {
// how can i get the pic url that i can use it when the app is starting ?
}
this is the way i loading photos right now : ( the path is taken from assets )
var img = UIImage(named:getAvatarPath());
btw im using swift 4 .
thanks .
You can save the image name or URL to UserDefaults.
Here is a code snippet to save URL string to UserDefaults.
UserDefaults.standard.set(avatarUrl, forKey: "userAvatar")
And retrieve the URL when displaying the image.
Something like this:
UserDefaults.standard.object(forKey: "userAvatar") as? String
You can also create a property in a class or struct by combining the above approaches to save/retrieve a property to/from UserDefaults.
for example:
var userAvatar = UserDefaults.standard.object(forKey: "userAvatar") as? String {
didSet {
UserDefaults.standard.set(avatarUrl, forKey: "userAvatar")
}
}
Now you can use this property to save or load the image URL string using UserDefaults.
Hope this helps
I'm trying to share a story with a background image a a sticker image via URL Scheme on my ios app, i am using the attached code and it dose not work.
When i'm trying to share just a background image or just a sticker it does work. But when im trying share both a background image and a sticker in top of it, it dose not work.
Any Ideas?
func shareToInstagram(deepLinkString : String){
let url = URL(string: "instagram-stories://share")!
if UIApplication.shared.canOpenURL(url){
let backgroundData = UIImageJPEGRepresentation(UIImage(named: "shop_placeholder")!, 1.0)!
let creditCardImage = UIImage(named: "share_instagram")!
let stickerData = UIImagePNGRepresentation(creditCardImage)!
let pasteBoardItems = [
["com.instagram.sharedSticker.backgroundImage" : backgroundData],
["com.instagram.sharedSticker.stickerImage" : stickerData],
]
if #available(iOS 10.0, *) {
UIPasteboard.general.setItems(pasteBoardItems, options: [.expirationDate: Date().addingTimeInterval(60 * 5)])
} else {
UIPasteboard.general.items = pasteBoardItems
}
UIApplication.shared.openURL(url)
}
I copy pasted OP's code for use in my own app (only substituting different UIImages) and found only 1 issue, pasteboard items should be contained in a single array otherwise instagram will render only the first item (in this case the background layer). To fix this, replace the declaration of pasteboard items with the following code
let pasteBoardItems = [
["com.instagram.sharedSticker.backgroundImage" : backgroundData,
"com.instagram.sharedSticker.stickerImage" : stickerData]
]
(basically just remove the close and open bracket separating the two items)
Also as a previous answer stated, make sure "instagram-stories" is included in LSApplicationQueriesSchemes in the info.plist file
I use this exact code in my app and it now works perfect
Everything is correct, my code is similar and it works for iOS 11+. I suggest you the following:
check the image data you pass to pasteboard (jpg can't be converted with
UIImagePNGRepresentation and vice versa)
check the info.plist. You should enable "instagram-stories" scheme in it (LSApplicationQueriesSchemes key)
Like Alec said, you need to put all of Instagram data in one list, not multiple lists. look at the example from the meta documents:
NSArray *pasteboardItems = #[#{#"com.instagram.sharedSticker.stickerImage" : stickerImage,
#"com.instagram.sharedSticker.backgroundTopColor" : backgroundTopColor,
#"com.instagram.sharedSticker.backgroundBottomColor" : backgroundBottomColor}];
2. For more recent readers, as of swift 4.2 and iOS 12 UIImageJPEGRepresentation is replaced by jpegData. change
let backgroundData = UIImageJPEGRepresentation(yourImage, 1.0)
with
let backgroundData = yourImage.jpegData(compressionQuality: 1.0)
I'm trying to obtain a video from the library in my swift app.
I'm using the following code snippet :
print("ChatDetailsController - imgaction - Vid from Lib pressed")
self.selection = "videoLib";
self.imgSelection = "vidFromLib"
self.imagePicker.sourceType = .PhotoLibrary
self.imagePicker.delegate = self
self.imagePicker.mediaTypes = ["public.movie"]
self.presentViewController(self.imagePicker!, animated: true, completion: nil)
Then in delegate method:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if(selection == "videoLib")
{
if let videoURL = info[UIImagePickerControllerReferenceURL] as? NSURL
{
print("imagePickerController - reached for video - from Lib - \(videoURL)")
let videoData = NSData(contentsOfURL: videoURL)
}
}
}
When running the app, it shows the library and I can select a video, then it says compressing video.. but then the app crashes.
The Crash happens when I try to access the videoData Variable becuase it wasn't able to get the actual data since the path is an asset path and not a video path.
The Log shows something similar to this:
fatal error: unexpectedly found nil while unwrapping an Optional value
The print function shows:
imagePickerController - reached for video - from Lib - assets-library://asset/asset.MOV?id=7D3BFA00-A7CB-470C-8899-F82FCC2CBC54&ext=MOV
Also, right after choosing/selecting the video, a message similar to this is displayed in the console/log:
[Generic] video path /var/mobile/Media/PhotoData/CPLAssets/group419/A2901DD1-FDEF-423B-B4A3-E808B9AC45E1.MOV for PLPreviewImageAndDurationForVideoAtPath does not exist
Of course the file name changes on every run.
I'm able to get a video and images from the camera, and getting images from the library, I'm only having trouble with using the the video from the library. I would expect to see the video URL similar to :
/var/mobile/Containers/Data/Application/BEDDC883-B20E-496D-8C9A-A40EC0A29214/Documents/63ea24932c101b76c72fcbae3993fc9b.mov for example; where the video file is in the application and not showing as an unusable asset URL.
Any help would be appreciated.
The problem is that you're trying to fetch the wrong video. You're trying to use info[UIImagePickerControllerReferenceURL]. What you want is info[UIImagePickerControllerMediaURL].
I am developing a share extension for photos for my iOS app. Inside the extension, I am able to successfully retrieve the UIImage object from the NSItemProvider.
However, I would like to be able to share the image with my container app, without having to store the entire image data inside my shared user defaults. Is there a way to get the PHAsset of the image that the user has chosen in the share extension (if they have picked from their device)?
The documentation on the photos framework (https://developer.apple.com/library/ios/documentation/Photos/Reference/Photos_Framework/) has a line that says "This architecture makes it easy, safe, and efficient to work with the same assets from multiple threads or multiple apps and app extensions."
That line makes me think there is a way to share the same PHAsset between extension and container app, but I have yet to figure out any way to do that? Is there a way to do that?
This only works if the NSItemProvider gives you a URL with the format:
file:///var/mobile/Media/DCIM/100APPLE/IMG_0007.PNG
which is not always true for all your assets, but if it returns a URL as:
file:///var/mobile/Media/PhotoData/OutgoingTemp/2AB79E02-C977-4B4A-AFEE-60BC1641A67F.JPG
then PHAsset will never find your asset. Further more, the latter is a copy of your file, so if you happen to have a very large image/video, iOS will duplicate it in that OutgoingTemp directory. Nowhere in the documentation says when it's going to be deleted, hopefully soon enough.
I think this is a big gap Apple has left between Sharing Extensions and PHPhotoLibrary framework. Apple should've be creating an API to close it, and soon.
You can get PHAsset if image is shared from Photos app. The item provider will give you a URL that contains the image's filename, you use this to match PHAsset.
/// Assets that handle through handleImageItem:completionHandler:
private var handledAssets = [PHAsset]()
/// Key is the matched asset's original file name without suffix. E.g. IMG_193
private lazy var imageAssetDictionary: [String : PHAsset] = {
let options = PHFetchOptions()
options.includeHiddenAssets = true
let fetchResult = PHAsset.fetchAssetsWithOptions(options)
var assetDictionary = [String : PHAsset]()
for i in 0 ..< fetchResult.count {
let asset = fetchResult[i] as! PHAsset
let fileName = asset.valueForKey("filename") as! String
let fileNameWithoutSuffix = fileName.componentsSeparatedByString(".").first!
assetDictionary[fileNameWithoutSuffix] = asset
}
return assetDictionary
}()
...
provider.loadItemForTypeIdentifier(imageIdentifier, options: nil) { imageItem, _ in
if let image = imageItem as? UIImage {
// handle UIImage
} else if let data = imageItem as? NSData {
// handle NSData
} else if let url = imageItem as? NSURL {
// Prefix check: image is shared from Photos app
if let imageFilePath = imageURL.path where imageFilePath.hasPrefix("/var/mobile/Media/") {
for component in imageFilePath.componentsSeparatedByString("/") where component.containsString("IMG_") {
// photo: /var/mobile/Media/DCIM/101APPLE/IMG_1320.PNG
// edited photo: /var/mobile/Media/PhotoData/Mutations/DCIM/101APPLE/IMG_1309/Adjustments/FullSizeRender.jpg
// cut file's suffix if have, get file name like IMG_1309.
let fileName = component.componentsSeparatedByString(".").first!
if let asset = imageAssetDictionary[fileName] {
handledAssets.append(asset)
imageCreationDate = asset.creationDate
}
break
}
}
}