UIImagePickerControllerReferenceURL returns nil, crashing the app - ios

I'm trying to print all the image metadata in the imagePickerController: didFinishPickingMediaWithInfo function. When I use the info.objectForKey(UIImagePickerControllerReferenceURL) method, it returns nil and if try to use this result my app crashes. Does anyone know why it returns nil and what else can I use to print all the image metadata when I pick an image? (using UIImageJPEGRepresentation is not an option because the EXIF data is removed).
This is my code:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!)
{
let image = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage
let refURL : NSURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL
var localSourceRef: CGImageSourceRef = CGImageSourceCreateWithURL(refURL, nil)
var localMetadata: NSDictionary = CGImageSourceCopyPropertiesAtIndex(localSourceRef, 0, nil)
println("\n Photo data: \n")
println(localMetadata)
}

So it sounds like there are actually two questions here:
1) Why is UIImagePickerControllerReferenceURL returning a nil reference?
2) How can you get location data from the photo?
So, the answer to (1) is usually because you receive the callback didFinishPickingMedia before the OS as written the file to the image library.
The answer to #2 is much trickier, as showcased by this question's line of answers:
Reading the GPS data from the image returned by the camera in iOS iphone
There are a number of variables you need to account for:
iOS will strip the GPS data out if you haven't requested access to location data, so you'll need to prompt for location access using CLLocationManager.
If the user has geotagging disabled, you'll never get GPS coords.
If the phone can't get a GPS lock, iOS won't record the GPS coords.
Per this answer: https://stackoverflow.com/a/10338012/490180 you should be able to retrieve the raw UIImage and then create the CGImageSourceRef from the data property off of UIImage's CGImage. This effectively removes the need for you to ever access the ReferenceURL.

Related

Getting metadata of image from photoLibrary

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

Save and retrieve Photos with all their metadata locally on iOS using swift

I am currently taking photos from the users iPhone making them jpegs, then saving them using file Manager and then saving their paths with core data.
The way I do it I can’t show live images or later export the image with its previous meta data.
My question is what is the correct way to save an image locally with all its meta data and live status so it can be displayed and exported later but only inside the app. I don’t want the images to be viable outside the application.
Taking The image:
Take a look at the UIImagePickerController class and use it to take pictures. Taking pictures with this, will NOT save the images automatically to the iOS gallery.
The mentioned UIImagePickerController will notify a delegate UIImagePickerControllerDelegate and call the function func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]).
From the second parameter, the info dictionary, you can request most of the images metadata, livePhoto and so on. The necessary keys can be found inside the extension of UIImagePickerController called UIImagePickerController.InfoKey.
For example, here is how I then retrieve the images coordinate if available:
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
if let asset = result.firstObject, let location = asset.location {
let lat = location.coordinate.latitude
let lon = location.coordinate.longitude
print("Here's the lat and lon \(lat) + \(lon)")
}
}
Saving the image onto the phone within your app?:
I use JSONEncoder. You can use this class to encode and then decode your custom image class again.
Your custom image class, which has the UIImage and all the other properties must then implement the Codable protocol.
Afterwards, use the JSONEncoder to create Data of the object via its encode() method and save it to an app private location with the help of a FileManager.
Later on, you may read all files from that location again with the FileManager, decode() with the JSONEncoder and voila you have all your images again in the form of your custom image class.
Saving the image to the users gallery "Exporting":
Here's an example of how I again save that image to the users gallery as a way of exporting it:
private static func saveImageToGallery(picture: UIImage, lat: Double?, lon: Double?) {
PHPhotoLibrary.shared().performChanges({
let request = PHAssetCreationRequest.creationRequestForAsset(from: picture)
if let _lat = lat, let _lon = lon {
request.location = CLLocation(latitude: _lat, longitude: _lon)
}
})
}
I hope this will be enough to guide you to the correct way.

Load image from the file after rewrite it - UIImage

I have silly problem with loading image from the file. I have two views putted to UITabBarController.
At the first view user can load his image from the Photo Library or Camera. Second view present this photo. This file is on the server. If user doesn't choose his image server sent custom image. If there is uploaded photo it will push user's picture.
When user tap button there is a menu with options. For example we will decide to take picture from the Photo Library. After user took image:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.saveUserImage(userID, imageData: UIImagePNGRepresentation(image)!)
apiManager.userUploadProfile(userID, imageData: UIImagePNGRepresentation(image)!)
userImageView.image = image
}
func saveUserImage(userUUID: String, imageData: NSData) {
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
let savePath = path! + "/\(userUUID)-user.png"
NSFileManager.defaultManager().createFileAtPath(savePath, contents: imageData, attributes: nil)
}
After that point user can see chosen picture and everything is okey. When user change tab, second view will refresh all data on it and again will download all images and data from the server. Unfortunately images that contains old user image doesn't refresh and there is still old photo.
When we come back to the first tab image is going back to old image but after few seconds.
The strangest thing is if I am checking server there is new uploaded image and in the app container it exist too. When I restart the app everything works perfectly.
It looks like this image is saved in the memory and iOS takes old version from RAM or from some other swap. How refresh UIImageView and show current saved image?
EDIT:
There is a method which load the image
func userProfilePicture(userId: String) -> UIImage {
let cacheDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last
let savePath = cacheDirectory! + "/\(userId)-user.png"
if NSFileManager.defaultManager().fileExistsAtPath(savePath) {
if let image = UIImage(named: savePath) {
return image
}
}
return UIImage(named: "test_avatar")!
}
I can't comment but i was facing a similar issue but i have a question, does the picture get uploaded before the user changes tabs, or after? Even if you call the function to update the database, it takes some time to actually "send the new photo/path to the photo to the database". Here are your options, if the picture is not in the server at the time of the switching tabs, implement a loading ui until it has successfully uploaded and the new, updated value, into the database. An easy way to do that would be to use the SVProgressHUD pod and set the default mask type to .clear which disables user interaction. Option 2, is to pass the actual UIImage via a static variable, or in the prepare for segue method, or through a struct, or any other way and set the picture that needs to be refreshed to the uiimage without getting it from the server only when you switch tabs.
Okey, I found the answer. The problem was in initialize UIImage. We have two methods to load image from file.
First one load the image and cache it in memory. Later it use only a reference to this image data in memory:
let image = UIImage(named: savePath)
Second method load image strictly from file every time when user use that function:
let image = UIImage(contentsOfFile: savePath)
Right now it works perfectly :D

iOS Swift 2 - Get path to video in camera roll

I'm working on an app that uploads a video on a server and I got to a point where I need to re-run the upload process if a user shuts down the app and openes it again later.
After a user taps on a video, this function gets triggered
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
if mediaType == kUTTypeMovie {
let fileUrl = info[UIImagePickerControllerMediaURL] as! NSURL
// ...
}
self.performSegueWithIdentifier("someSegue", sender: self)
}
But I found out that fileUrl points to some temporary reference to the original video and is always different even if the video is the same.
So how can I get real path to the video, which I can save to NSUserDefaults so when a user opens the app again I can access the video again.
The UIImagePicker will always compress a video and hands you the NSURL of the temp file given in info[UIImagePickerControllerMediaURL]--and it is your responsibility to delete that temp file whether you use it or not.
If you want to use the compressed version, move it to your documents folder and save the path to that in your NSUserDefaults.
If you want the original raw video, get the NSURL in info[UIImagePickerControllerReferenceURL]. This is the URL to the original ALAsset. Then you'll need to use PHImageManager().requestImageDataForAsset or PHImageManager().requestExportSessionForVideo to get the data for upload.

Save image with the correct orientation - Swift & Core Image

I'm using Core Image in Swift for editing photos and I have a problem when I save the photo. I'm not saving it with correct orientation.
When I get the picture from the Photo Library I'm saving the orientation in a variable as UIImageOrientation but I don't know how to set it back before saving the edited photo to the Photo Library. Any ideas how?
Saving the orientation:
var orientation: UIImageOrientation = .Up
orientation = gotImage.imageOrientation
Saving the edited photo to the Photo Library:
#IBAction func savePhoto(sender: UIBarButtonItem) {
let originalImageSize = CIImage(image:gotImage)
filter.setValue(originalImageSize, forKey: kCIInputImageKey)
// 1
let imageToSave = filter.outputImage
// 2
let softwareContext = CIContext(options:[kCIContextUseSoftwareRenderer: true])
// 3
let cgimg = softwareContext.createCGImage(imageToSave, fromRect:imageToSave.extent())
// 4
let library = ALAssetsLibrary()
library.writeImageToSavedPhotosAlbum(cgimg,
metadata:imageToSave.properties(),
completionBlock:nil)
}
Instead of using the metadata version of writeImageToSavedPhotosAlbum, you can use :
library.writeImageToSavedPhotosAlbum(
cgimg,
orientation: orientation,
completionBlock:nil)
then you can pass in the orientation directly.
To satisfy Swift, you may need to typecast it first:
var orientation : ALAssetOrientation = ALAssetOrientation(rawValue:
gotImage.imageOrientation.rawValue)!
As per my somewhat inconclusive answer here.
(As you have confirmed, this solution does indeed work in Swift - I derived it, untested, from working Objective-C code)
If you are interested in manipulating other information from image metadata, here are a few related answers I provided to other questions...
Updating UIImage orientation metaData?
Force UIImagePickerController to take photo in portrait orientation/dimensions iOS
How to get author of image in cocoa
Getting a URL from (to) a "picked" image, iOS
And a small test project on github that digs out image metadata from various sources (it's objective-C, but the principles are the same).
You are calling writeImageToSavedPhotosAlbum:metadata:completionBlock:... The docs on that method say:
You must specify the orientation key in the metadata dictionary to preserve the orientation of the image.

Resources