- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个应用程序,直到几天前我还在开发它,它只允许某人从他们的照片库上传照片。它运行良好,检查 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/
我有一个 UIImagePickerController 和一个 cameraOverlayView,但是当用户拍照时,cameraOverlay View 会保留在“预览/使用照片/重拍照片 Vie
我试过将 Info.plist 'View controller-based status bar appearance' 设置为 NO,我试过调用 [[UIApplication sharedApp
我正在尝试快速使用 UIImagePickerController 但不起作用... 我的 View Controller : class ViewController: UIViewControll
我的 UIImagePickerController 有问题,allowEditing = YES。 我无法从底部裁剪图像,但在移动裁剪矩形时,顶部还有一个额外的空白空间。 也在方法中。 -(void
为什么当 UIImagePickerController 处于视频模式时,AVFoundation 显示的缩放级别与 UIImagePickerController 相同?如何使 AVFoundati
我正在开发一个应用程序,我必须使用图像选择器 Controller 从手机库导入图像。问题是我必须首先关闭选择器 Controller ,然后在选择器 Controller 关闭后呈现另一个 Cont
我的以下代码在 swift 4 上运行良好,但在升级到 swift 4.2 后出现此错误,我浪费了 3 个小时搜索问题所在但失败了。如果有人能指导我如何解决这个问题,请多多指教。 func image
所以我正在尝试为 iOS 7 更新一个应用程序,但我遇到了自定义叠加层的问题。叠加层是我用来构图的图像(实时和使用全分辨率版本在相机胶卷中构图最终结果)。问题是,现在在 iOS 7 下,覆盖层虽然在底
我正在使用 UIImagePickerController 捕获图像然后存储它。但是,当我尝试重新缩放它时,我从该图像中获得的方向值不正确。当我拿起手机拍照时,它给了我左边的方向。有没有人遇到过这个问
我注意到 UIImagePicker 为相机胶卷返回的 NSDictionary 似乎不再返回 UIImagePickerControllerOriginalImage 对象。相反,我得到了一个 UI
我有一个简单的标准图像选择器,它工作正常,但当我使用编辑器功能时,图像会移动到方形裁剪框上方。当我选择图像或调整图像大小然后接受它时,裁剪后的图像在裁剪框上方有大约 20 个像素的图像,底部有大约 4
我的应用程序允许用户从设备的相机胶卷中选择图像。我想验证所选图像的格式是 PNG 或 JPG 图像。 是否可以在 - (void)imagePickerController:(UIImagePicke
是否可以保存视频并将其添加到自定义 ALAsset,以 mp4 格式从 UIImagePicker 捕获?或者我必须将它保存在 .mov 中并通过 AVAssetExportSession 进行压缩?
似乎内置的 UIImagePickerController 无法接受除设备相机卷以外的来源。 我想在我自己的应用程序中获得选择和放大图片的功能。另外我想允许用户选择图片并将其保存到他们的相机胶卷中(以
我想在呈现 UIImagePickerController 拍照时隐藏显示快门打开的动画。我检查了this question对于包含已接受的内容的相同内容,但我无法在我的 Controller 中复制
我想 UIImagePickerController 会允许您使用相机或从 View 中的库中进行选择?看来我必须选择一个或另一个 cia 的 sourceType?有没有办法在选择库 View 中添
我试图在 UIActionSheet 中单击按钮后显示 UIImagePickerController。代码很简单。但是,对 [[UIImagePickerController alloc] init
我搜索了一些在我的应用程序中使用 UIImagePickerController 的方法,我发现唯一有效的方法如下: 在应用程序显示的第一个 View 上,输入: self.picker = [[UI
UIImagePickerController 很容易使用,但是当我以前没有发现它时,我突然发现它很令人恼火。发生的情况是,有时 imagePickerController:didFinishPick
我有以下代码: SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondVie
我是一名优秀的程序员,十分优秀!