在iOS中使用照片保存位置exif数据

时间:2016-02-24 13:23:52

标签: ios swift image gps exif

我正在构建一个需要拍照并使用GPS信息和时间戳保存到图书馆的应用。

一切似乎都在起作用,包括添加位置元数据的代码(调试窗口中所有成功的消息)。

但是,当我检查照片的属性时,没有位置元数据。



var locManager = CLLocationManager();
@IBOutlet weak var photoImageView: UIImageView!

@IBAction func addPhotoFromCameraButton(sender: AnyObject) {
    locManager.requestWhenInUseAuthorization();
    locManager.startUpdatingLocation();
    if !CLLocationManager.locationServicesEnabled() {
        print("LOCATION SERVICES DISABLED");
        return;
    }    
    imagePicker = UIImagePickerController();
    imagePicker.delegate = self;
    imagePicker.sourceType = .Camera;
    presentViewController(imagePicker, animated: true, completion: nil);
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imagePicker.dismissViewControllerAnimated(true, completion: { () in
        if picker.sourceType == .Camera {
            let image = info[UIImagePickerControllerOriginalImage] as! UIImage;
            UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil);
        }
    });
}

func image(image: UIImage, didFinishSavingWithError: NSErrorPointer, contextInfo: UnsafePointer<Void>) {
    if didFinishSavingWithError != nil {
        print("Error saving photo: \(didFinishSavingWithError)");
    }
    else {
        print("Successfully saved photo, will make request to update asset metadata");
        // fetch the most recent image asset
        let fetchOptions = PHFetchOptions();
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)];
        let fetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions);
            
        // get the asset we want to modify from results
        let lastImageAsset = fetchResult.lastObject as! PHAsset;
        //self.photoImageView.image = getAssetThumbnail(lastImageAsset);
        
        // get location
        var myLocation = CLLocation();
        if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
            CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways) {
            myLocation = locManager.location!;
        }
            
        // make change request
        PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            // modify existing asset
            let assetChangeRequest = PHAssetChangeRequest(forAsset: lastImageAsset);
            assetChangeRequest.location = myLocation;
            },
            completionHandler: {
                (success:Bool, error:NSError?) -> Void in
                if success {
                    print("Successfully saved metadata to asset");
                    print("Location metadata = \(myLocation)");
                    return;
                }
                else {
                    print("Failed to save metadata to asset with error: \(error!)");
                    return;
                }
        });
    }
}
&#13;
&#13;
&#13;

0 个答案:

没有答案