我有一个我正在研究的应用程序直到几天前才允许某人从他们的照片库上传照片。它工作正常,并检查GPS坐标的元数据并进行条件比较。
然而,现在,我决定让他们通过应用程序拍摄照片,并遇到障碍。使用相机时,用于拍照的代码效果不佳。
以下是我上传照片按钮的原始代码:
@IBAction func pickPhoto(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) {
imageView.hidden = true
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePicker.allowsEditing = false
presentViewController(imagePicker, animated: true, completion: nil)
}
}
然后是UIImagePickerController:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
println("imagePickerController")
let library = ALAssetsLibrary()
let url: NSURL = info[UIImagePickerControllerReferenceURL] as! NSURL
library.assetForURL(url, resultBlock: {
(asset: ALAsset!) in
if asset.valueForProperty(ALAssetPropertyLocation) != nil {
println("!= nil")
let imageLongitude = (asset.valueForProperty(ALAssetPropertyLocation) as! CLLocation!).coordinate.longitude
let imageLatitude = (asset.valueForProperty(ALAssetPropertyLocation) as! CLLocation!).coordinate.latitude
println("imageLatitude = \(imageLatitude)")
println("imageLongitude = \(imageLongitude)")
println("Starting the Location check")
......然后从那里开始做更多的检查。
我复制了我的pickPhoto按钮并将其转换为takePhoto按钮,如下所示:
@IBAction func takePhoto(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
imageView.hidden = true
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.allowsEditing = false
presentViewController(imagePicker, animated: true, completion: nil)
} else {
notifyUser("No Camera", message: "This device doesn't have a camera!")
}
}
然后打开相机并让我拍照。但是,当我使用照片时,我立即在UIImagePickerController上发生了崩溃:
let url: NSURL = info[UIImagePickerControllerReferenceURL] as! NSURL
我认为这是因为作为相机图像,它在技术上不会保存在任何地方,因此它没有URL或路径。
所以我的问题是,我如何保存图像(暂时或在相机胶卷中),保持相机中的GPS元数据完整(假设位置服务处于活动状态),所以我可以传递它并让它玩得很好?
答案 0 :(得分:1)
正如您已经注意到的那样,您需要将文件保存到磁盘才能获取其URL。您必须将其保存到相机SavedPhotosAlbum或您的文档文件夹。您还需要保存图像元数据信息)UIImagePickerControllerMediaMetadata)如下:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
print(info["UIImagePickerControllerOriginalImage"] ?? "NO IMAGE")
print(info["UIImagePickerControllerReferenceURL"] ?? "NO URL")
print(info["UIImagePickerControllerMediaMetadata"] ?? "NO METADATA")
if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
ALAssetsLibrary().writeImageToSavedPhotosAlbum(image.CGImage!, metadata: info[UIImagePickerControllerMediaMetadata]! as! [NSObject : AnyObject], completionBlock: { (url, error) -> Void in
print("photo saved to asset")
print(url) // assets-library://asset/asset.JPG?id=CCC70B9F-748A-43F2-AC61-8755C974EE15&ext=JPG
// you can load your UIImage that was just saved to your asset as follow
let assetLibrary = ALAssetsLibrary()
assetLibrary.assetForURL(url,
resultBlock: { (asset) -> Void in
if let asset = asset {
let assetImage = UIImage(CGImage: asset.defaultRepresentation().fullResolutionImage().takeUnretainedValue())
print(assetImage)
}
}, failureBlock: { (error) -> Void in
if let error = error { print(error.description) }
})
if let error = error { print(error.description) }
self.dismissViewControllerAnimated(true, completion: nil)
})
}
}