gpt4 book ai didi

ios - UIImagePickerController 相机不提供​​ URL

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:16 24 4
gpt4 key购买 nike

我有一个应用程序,直到几天前我还在开发它,它只允许某人从他们的照片库上传照片。它运行良好,检查 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 元数据完好无损(假设位置服务处于事件状态),这样我就可以传递它并让它很好地播放?

最佳答案

编辑:

这篇文章已过时


swift 2.x

正如您已经注意到的,您需要将文件保存到磁盘才能获取其 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)
})
}
}

关于ios - UIImagePickerController 相机不提供​​ URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30768384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com