简体   繁体   English

快速获取文件名UIImagePickerController

[英]Get filename UIImagePickerController in swift

How do you get the filename of the picture taken from the camera with swift? 如何快速获取从相机拍摄的照片的文件名? I need the filename because I want to get the path and send it to the web server. 我需要文件名,因为我想获取路径并将其发送到Web服务器。

let path = NSBundle.mainBundle().pathForResource("imageName", ofType: "jpg") as String

Thanks. 谢谢。

Please allow me to sort your question and offer different alternatives: 请允许我对您的问题进行排序,并提供其他替代方法:

When you take pictures with an iOS device by using the UIImagePickerController or any other available controller, the image is usually saved in a UIImage object and not necessary to the bundle documents folder in the app, unless you decide to do it in the code for further display. 当您通过使用UIImagePickerController或任何其他可用的控制器在iOS设备上拍照时,图像通常保存在UIImage对象中,而不需要保存在应用程序的捆绑文档文件夹中,除非您决定在代码中进一步进行处理显示。 Regarding uploading the image to a web server, it depends on the destination server requirements, therefore, you should check what are the methods available for you to upload the image to that server. 关于将映像上传到Web服务器,这取决于目标服务器的要求,因此,您应该检查哪些方法可用于将映像上传到该服务器。 I'm familiar with few options as follow: 我熟悉以下几种选择:

1) The destination server requires to deliver the image in multipart/form-data, then you should convert your UIImage to NSData by using one to the two available methods UIImageJPEGRepresentation or UIImagePNGRepresentation depending in which format you are required to send. 1)目标服务器需要以multipart / form-data形式交付图像,然后应使用UIImageJPEGRepresentation或UIImagePNGRepresentation两种可用方法之一,将UIImage转换为NSData,具体取决于您要发送的格式。 I suggest you check how to build the call to the destination server method (there are several post in StackOverflow about it) 我建议您检查如何构建对目标服务器方法的调用(StackOverflow中有几篇关于它的文章)

2) Server requires to deliver the image in base64EncodedString mode, then you need to convert your image as in the following example: 2)服务器需要以base64EncodedString模式传送图像,然后需要转换图像,如以下示例所示:

let string: String = "data:image/png;base64," + (UIImagePNGRepresentation(image!)?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength))!

3)The destination server requires a path (meaning they don't store the image): you should first save the picture in an accessible path in the web (not inside the iOS device) by probably using one of the 2 methods I just described above and then, upload the image path you get from the place you uploaded the picture to the destination server. 3)目标服务器需要一个路径(这意味着它们不存储图像):您应该首先使用可能刚才描述的2种方法之一将图片保存在网络中的可访问路径中(而不是iOS设备内部)。然后,将您从将图片上传到目标服务器的位置获得的图像路径上传到目标服务器。

I hope I succeeded to help you with my answer :-) 希望我能成功地帮助您:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM