1

I'm using UIImagePickerController to take photos with camera and also to get photos from SavedPhotosAlbum library. Once user takes a photo I save it in SavedPhotosAlbum and the following method is called:

override func image(image: UIImage, didFinishSavingWithError: NSErrorPointer, contextInfo:UnsafePointer<Void>)       {

        if (didFinishSavingWithError != nil) {
            print("Error saving photo: \(didFinishSavingWithError)")
        } else {
            let photoToSend = CompressAndSendPhoto(image: image)
            photoToSend.uploadImageRequest()
            print("Successfully saved photo, will make request to update asset metadata")

            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
            let fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
            let lastImageAsset = fetchResult.lastObject as! PHAsset
            let coordinate = CLLocationCoordinate2DMake(self.coordinate1, self.coordinate2)
            let nowDate = NSDate()
            let myLocation = CLLocation(coordinate: coordinate, altitude: 0.0, horizontalAccuracy: 1.0, verticalAccuracy: 1.0, timestamp: nowDate)

            PHPhotoLibrary.sharedPhotoLibrary().performChanges({

                let assetChangeRequest = PHAssetChangeRequest(forAsset: lastImageAsset)
                assetChangeRequest.location = myLocation

                }, completionHandler: {
                    (success:Bool, error:NSError?) -> Void in

                    if (success) {
                        print("Succesfully saved metadata to asset")
                        print("location metadata = \(myLocation)")
                    } else {
                        print("Failed to save metadata to asset with error: \(error!)")
                    }
            });
        }
    }

and it works fine, user current location is being added to the photo asset. The problem is that I can not get this value while choosing a photo from SavedPhotosAlbum. I googled many options but none of them works. How can I do it in method below?

  func imagePickerController(
        picker: UIImagePickerController,
        didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

            if picker.sourceType == UIImagePickerControllerSourceType.SavedPhotosAlbum {


            } 


        dismissViewControllerAnimated(true, completion: nil)
    }

Also I would like to add more "fields" to photo asset, not only location which is one of default ones, how can I add custom NSDictionary of values?

dammarpol
  • 77
  • 1
  • 13
  • ok, I found out how to do it, I just use simple function: func imageFromAsset(nsurl: NSURL) { let asset = PHAsset.fetchAssetsWithALAssetURLs([nsurl], options: nil).firstObject as! PHAsset print("location: " + String(asset.location)) } – dammarpol Nov 02 '15 at 08:08

0 Answers0