I have tried multiple ways to get the location of an image which took through the UIImagePickerController Camera.
What I want to achieve is that, I want to select an image using UIImagePickerController Camera and I have to save it into Photo Library so that only I can take back the PHAsset from it and also the location associated with it.
//MARK: Saving an Image to PhotoLibrary and taking back the PHAsset
class func savingThis(image : UIImage, completion : (asset : PHAsset?) -> ())
{
var localIdentifier : String?
let imageManager = PHPhotoLibrary.sharedPhotoLibrary()
imageManager.performChanges({ () -> Void in
let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
if let properAsset = request.placeholderForCreatedAsset {
localIdentifier = properAsset.localIdentifier
}
else {
completion(asset: nil)
}
}, completionHandler: { (success, error) -> Void in
if let properLocalIdentifier = localIdentifier {
let result = PHAsset.fetchAssetsWithLocalIdentifiers([properLocalIdentifier], options: nil)
if result.count > 0 {
completion(asset: result[0] as? PHAsset)
}
else {
completion(asset: nil)
}
}
else {
completion(asset: nil)
}
})
}
I have tried this code, to save and get back the PHAsset. But the problem is that this PHAsset does not have any location associated with it, wondering why? And what I missed?
I believe that I don't have to manually set GPS data into image's metadata right? I think that Photos Framework or Asset Library takes care of it. So as you know that Asset Library is deprecated our only option is to use Photos Framework. I read online that, Saving image to Photo Library takes care of it. Isn't it correct?
Is there any alternative? Should I use UIImageWriteToSavedPhotosAlbum method to save image to Camera Roll, And I can take back the very recent photo using Photos Framework. But I don't think that UIImageWriteToSavedPhotosAlbum will take care of the location thing.
Do you have any thoughts?
First of all thanking all who were all kind to take a look into the question.
I found my answer.
//MARK: Saving an Image to PhotoLibrary and taking back the PHAsset
class func savingThis(image : UIImage, completion : (asset : PHAsset?) -> ())
{
var localIdentifier : String?
let imageManager = PHPhotoLibrary.sharedPhotoLibrary()
imageManager.performChanges({ () -> Void in
let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
request.location = // Assigned current location here :)
if let properAsset = request.placeholderForCreatedAsset {
localIdentifier = properAsset.localIdentifier
}
else {
completion(asset: nil)
}
}, completionHandler: { (success, error) -> Void in
if let properLocalIdentifier = localIdentifier {
let result = PHAsset.fetchAssetsWithLocalIdentifiers([properLocalIdentifier], options: nil)
if result.count > 0 {
completion(asset: let asset = result[0] as? PHAsset)
}
else {
completion(asset: nil)
}
}
else {
completion(asset: nil)
}
})
}
When you get a callback from UIImagePickerController:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo
info: [UIImagePickerController.InfoKey : Any]) {}
you can try to get the image location from the info dictionary.
For that, you need to request the related PHAsset object:
if let assetImage = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
print("Image location info = \( assetImage.location))")
}
Important - before using this approach you need to request the user permission:
PHPhotoLibrary.requestAuthorization()
If you do not do it the info dictionary for UIImagePickerController.InfoKey.phAsset will return nil.
Related
I have successfully saved an image to Photo Library using Photo Framework API's after user selected that image from library using UIImagePickerController.
As I am capturing image inside my app, I have to take care of the metadata. I could save location & creationDate to the newly captured image and saved it into library.
But the problem is that I want to attach a ton of other properties, I mean I want to attach EXIF & TIFF properties.
There are a lot of properties in EXIF like this,
kCGImagePropertyExifDateTimeOriginal
kCGImagePropertyExifMaxApertureValue
kCGImagePropertyExifSpatialFrequencyResponse
kCGImagePropertyExifLensModel
How am I supposed to get these values from the current device and attach it to image and save it to Photo Library?
Here is the complete code which I am using to save an Image to Photo Library after taking it from UIImagePickerController. I would like to put attaching metadata lines somewhere in this function before saving it to Photo Library.
//MARK: Saving an Image to PhotoLibrary and taking back the PHAsset
class func savingThis(image : UIImage, completion : (asset : PHAsset?) -> ())
{
var localIdentifier : String?
let imageManager = PHPhotoLibrary.sharedPhotoLibrary()
imageManager.performChanges({ () -> Void in
let request = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
request.location = LocationManager.sharedInstance.locationManager.location
request.creationDate = NSDate()
// Add more properties here - somewhere EXIF, TIFF....
if let properAsset = request.placeholderForCreatedAsset {
localIdentifier = properAsset.localIdentifier
}
else {
completion(asset: nil)
}
}, completionHandler: { (success, error) -> Void in
if let properLocalIdentifier = localIdentifier {
let result = PHAsset.fetchAssetsWithLocalIdentifiers([properLocalIdentifier], options: nil)
if result.count > 0 {
completion(asset: result[0] as? PHAsset)
}
else {
completion(asset: nil)
}
}
else {
completion(asset: nil)
}
})
}
Is this a dumb question? I don't have to use Photos Framework if there is another way to attach metadata, whatever way, I just wanna attach it. I don't know. Let me know your suggestions?
Now that AssetsLibrary has been deprecated, we're supposed to use the photos framework, specifically PHPhotoLibrary to save images and videos to a users camera roll.
Using ReactiveCocoa, such a request would look like:
func saveImageAsAsset(url: NSURL) -> SignalProducer<String, NSError> {
return SignalProducer { observer, disposable in
var imageIdentifier: String?
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let changeRequest = PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL(url)
let placeholder = changeRequest?.placeholderForCreatedAsset
imageIdentifier = placeholder?.localIdentifier
}, completionHandler: { success, error in
if let identifier = imageIdentifier where success {
observer.sendNext(identifier)
} else if let error = error {
observer.sendFailed(error)
return
}
observer.sendCompleted()
})
}
}
I created a gif from a video using Regift and I can verify that the gif exists inside my temporary directory. However when I go save that gif to the camera roll, I get a mysterious error: NSCocoaErrorDomain -1 (null), which is really super helpful.
Has anyone ever experienced this issue?
You can try this.
let data = try? Data(contentsOf: /*Your-File-URL-Path*/)
PHPhotoLibrary.shared().performChanges({
PHAssetCreationRequest.forAsset().addResource(with: .photo, data: data!, options: nil)
})
I have an app in which the user can record a video, this video is saved in the photo gallery, and I store the path to the video so that in the future the user could see again the video inside the app. The problem is that the method that I use I think it's giving me some kind of temporary path, and after some days, the video still in the gallery, but the path is not valid anymore and make the app crash. This is the code I'm using:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let mediaType: String = info["UIImagePickerControllerMediaType"] as! String
if mediaType == "public.movie" {
let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL!
let pathString = tempImageURL.relativeString
self.dismissViewControllerAnimated(true, completion: {})
if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary {
self.videoPath = pathString
// Save the path in the DB
} else {
VideoManager.saveVideo(tempImageURL, onComplete: { (path) -> Void in
self.videoPath = path
// Save the path in the DB
})
var fileManager: NSFileManager = NSFileManager()
fileManager.removeItemAtPath(pathString!, error: nil)
}
}
}
And the VideoManager.saveVideo method code is the following:
func saveVideo(videoURL: NSURL, onComplete:((path: String) -> Void)) {
var assetsLibrary: ALAssetsLibrary = ALAssetsLibrary()
assetsLibrary.writeVideoAtPathToSavedPhotosAlbum(videoURL, completionBlock: { (assetURL: NSURL!, error: NSError!) -> Void in
var path: String = error == nil ? "\(assetURL)" : kEmptyString
onComplete(path: path)
})
}
I don't know what I'm doing wrong, I've tried with the method UISaveVideoAtPathToSavedPhotosAlbum but without success.. Any ideas?
For giving a little more information, when the video is selected from the gallery, the url I get is like the following one:
file:///private/var/mobile/Containers/Data/Application/D2E8E31B-CEA0-43B0-8EF9-1820F6BDE4A9/tmp/trim.AD855155-AB78-4A16-9AA8-DF2B3F39824E.MOV
And when I record a new video using the camera, first I have this URL:
file:///private/var/mobile/Containers/Data/Application/D2E8E31B-CEA0-43B0-8EF9-1820F6BDE4A9/tmp/capture/capturedvideo.MOV
and when I do writeVideoAtPathToSavedPhotosAlbum it returns an URL like:
assets-library://asset/asset.MOV?id=958507B5-1353-4DDC-BC07-D9CBC6126657&ext=MOV
Both of them work, but some days later stop working.
OK finally I found the solution, the thing was that you can't access directly the photo gallery with the stored url, you got to use the assetLibrary.assetForURL method, which I was missing. In the end the imagepickercontroller delegate method is like this:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let mediaType: String = info["UIImagePickerControllerMediaType"] as! String
if mediaType == "public.movie" {
self.dismissViewControllerAnimated(true, completion: {})
if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary {
let tempImageURL = info[UIImagePickerControllerReferenceURL] as! NSURL!
self.videoPath = tempImageURL.absoluteString
} else {
let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL!
VideoManager.saveVideo(tempImageURL, onComplete: { (path) -> Void in
self.videoPath = path
})
}
}
}
I also was missing that when you record a video, you got to obtain the url using:
let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL!
But when you get the video you have to do:
let tempImageURL = info[UIImagePickerControllerReferenceURL] as! NSURL!
Hope it helps!!
I'm trying to do the equivalent of writeImageToSavedPhotosAlbum with the new Photo framework.
To save the image, I only do this:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage] as UIImage
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
let changeRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
}, completionHandler: { (success, error) -> Void in
//
})
}
Of course, there's no magic and since I don't do anything of
info[UIImagePickerControllerMediaMetadata]
, the above code doesn't save any metadata to the "Camera Roll", as you can see using the screenshot of the Preview.app when I connect my iPhone to my Mac.
You get that view by opening Preview.app, selecting File > Import from "you device name"; then you can browse your pictures and see that those taken with the Camera app show exif data such as focal length, while those saved with the above code show empty values.
Now the documentation for creationRequestForAssetFromImage says:
To set metadata properties of the newly created asset, use the
corresponding properties of the change request (listed in Modifying
Assets).
Which links to "+changeRequestForAsset:" and 4 properties (creationDate, location, favorite, hidden), that's a little light. What about the other properties one might want to save (like aperture, focal length, shutter speed, …)?
How are you supposed to save your meta data along the image with the Photo framework?
Here's what I ended up doing:
extension UIImage {
/**
Gets the metadata from the photo album
:param: info The picker dictionary
:param: completionHandler A block to call when the metadata is available
*/
class func requestMetadata(info: [NSObject : AnyObject], completionHandler: ([NSObject : AnyObject]) -> Void) {
let assetUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
let result = PHAsset.fetchAssetsWithALAssetURLs([assetUrl], options: nil)
if let asset = result.firstObject as? PHAsset {
let editOptions = PHContentEditingInputRequestOptions()
editOptions.networkAccessAllowed = true
asset.requestContentEditingInputWithOptions(editOptions, completionHandler: { (contentEditingInput, _) -> Void in
let url = contentEditingInput.fullSizeImageURL
let orientation = contentEditingInput.fullSizeImageOrientation
var inputImage = CoreImage.CIImage(contentsOfURL: url)
completionHandler(inputImage.properties())
})
} else {
completionHandler([:])
}
}
}
I have a hard time because I want to extract the GPS coordinates from a photo. I use the function imagePickerController:didFinishPickingMediaWithInfo to pick an image and the I am inserting that image in a collectionView using the new Photos framework.
I want to extract the GPS coordinates from the photo. I have done some research and I am aware of the fact that UIImage does not contain all the metadata, so I tried using the AssetsLibrary framework.
Inside didFinishPickingMediaWithInfo I am using the following code to extract the photo location:
var referenceURL : NSURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL
var library : ALAssetsLibrary = ALAssetsLibrary()
library.assetForURL(referenceURL, resultBlock: { (asset : ALAsset!) -> Void in
var rep : ALAssetRepresentation = asset.defaultRepresentation()
var metadata : NSDictionary = rep.metadata()
let location: AnyObject! = asset.valueForProperty(ALAssetPropertyLocation)
if location != nil {
println(location)
}
else
{
println("Location not found")
}
})
{
(error : NSError!) -> Void in
}
However, it doesn't find the location, even though I checked the image and it contains EXIF metadata (it contains also GPS locations, in which I am interested in). How can I retrieve the coordinates from photo?
ALAssetsLibrary is deprecated in iOS 10. Fortunately, with Photos framework, this is trivial to implement. When imagePickerController(_ picker:, didFinishPickingMediaWithInfo) is called, you can retrieve location information through a simple lookup. Take a look at the code below:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let URL = info[UIImagePickerControllerReferenceURL] as? URL {
let opts = PHFetchOptions()
opts.fetchLimit = 1
let assets = PHAsset.fetchAssets(withALAssetURLs: [URL], options: opts)
let asset = assets[0]
// The location is "asset.location", as a CLLocation
// ... Other stuff like dismiss omitted
}
Hope this helps. This is Swift 3, of course..
I found a solution using the following code:
if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary
{
if let currentLat = pickedLat as CLLocationDegrees?
{
self.latitude = pickedLat!
self.longitude = pickedLong!
}
else
{
var library = ALAssetsLibrary()
library.enumerateGroupsWithTypes(ALAssetsGroupAll, usingBlock: { (group, stop) -> Void in
if (group != nil) {
println("Group is not nil")
println(group.valueForProperty(ALAssetsGroupPropertyName))
group.enumerateAssetsUsingBlock { (asset, index, stop) in
if asset != nil
{
if let location: CLLocation = asset.valueForProperty(ALAssetPropertyLocation) as CLLocation!
{ let lat = location.coordinate.latitude
let long = location.coordinate.longitude
self.latitude = lat
self.longitude = lat
println(lat)
println(long)
}
}
}
} else
{
println("The group is empty!")
}
})
{ (error) -> Void in
println("problem loading albums: \(error)")
}
}
}
What it does is that it reads the entire album and prints the location if the photo has that property, else it prints "location not found". It does so for every photo in the album.So I have another question... I want to display the location info just for the photo that I have selected, not for the entire album. Does anyone have a clue how this can be accomplished?
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
pickedImage.image = image
let url = info[UIImagePickerControllerReferenceURL] as! NSURL
let library = ALAssetsLibrary()
library.assetForURL(url, resultBlock: { (asset) in
if let location = asset.valueForProperty(ALAssetPropertyLocation) as? CLLocation {
self.latitude = location.coordinate.latitude
self.longitude = location.coordinate.longitude
}
}, failureBlock: { (error: NSError!) in
print("Error!")
})
}
self.dismissViewControllerAnimated(true, completion: nil)
}
Finally managed to get this after trying a lot of different ways, it's remarkably poorly referenced in the apple documentation (or I just couldn't find it). Unfortunately any images that weren't taken through the stock "camera" app don't tend to have location metadata. But it works fine when they do.
Got the answer from https://stackoverflow.com/a/27556241/4337311
With iOS 11 everything becomes much easier to recover GPS coordinate from a picture in your photo roll.
First import the Photo SDK
import Photos
Then before pushing the UIImagePickerController, ask the authorisation to access the data :
if PHPhotoLibrary.authorizationStatus() == .notDetermined {
PHPhotoLibrary.requestAuthorization { [weak self](_) in
// Present the UIImagePickerController
self?.present(picker, animated: true, completion: nil)
}
}
Then when you grab back the image from the UIImagePickerController, you only need to do the following to grab the coordinates :
let coordinate = (info[UIImagePickerControllerPHAsset] as? PHAsset)?.location?.coordinate
iOS 12 & 13
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// For Location
if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
print("============> \(asset.location?.coordinate.longitude ?? 0) \(asset.location?.coordinate.latitude ?? 0)")
}
}