我有一个应用程序,直到几天前我还在开发它,它只允许某人从他们的照片库上传照片。它运行良好,检查 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/
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在使用Rails构建一个简单的聊天应用程序。当用户输入url时,我希望将其输出为html链接(即“url”)。我想知道在Ruby中是否有任何库或众所周知的方法可以做到这一点。如果没有,我有一些不错的正则表达式示例代码可以使用... 最佳答案 查看auto_linkRails提供的辅助方法。这会将所有URL和电子邮件地址变成可点击的链接(htmlanchor标记)。这是文档中的代码示例。auto_link("Gotohttp://www.rubyonrails.organdsayhellotodavid@loudthinking.
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
我正在编写一个简单的静态Rack应用程序。查看下面的config.ru代码:useRack::Static,:urls=>["/elements","/img","/pages","/users","/css","/js"],:root=>"archive"map'/'dorunProc.new{|env|[200,{'Content-Type'=>'text/html','Cache-Control'=>'public,max-age=6400'},File.open('archive/splash.html',File::RDONLY)]}endmap'/pages/search.
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
如何使此根路径转到:“/dashboard”而不仅仅是http://example.com?root:to=>'dashboard#index',:constraints=>lambda{|req|!req.session[:user_id].blank?} 最佳答案 您可以通过以下方式实现:root:to=>redirect('/dashboard')match'/dashboard',:to=>"dashboard#index",:constraints=>lambda{|req|!req.session[:user_id].b
📢博客主页:https://blog.csdn.net/weixin_43197380📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!📢本文由Loewen丶原创,首发于CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨文章预览:一.分辨率(Resolution)1、工业相机的分辨率是如何定义的?2、工业相机的分辨率是如何选择的?二.精度(Accuracy)1、像素精度(PixelAccuracy)2、定位精度和重复定位精度(RepeatPrecision)三.公差(Tolerance)四.课后作业(Post-ClassExercises)视觉行业的初学者,甚至是做了1~2年