如何上传从 UIImagePickerController 拍摄的图像 [英] How to upload image that was taken from UIImagePickerController

查看:26
本文介绍了如何上传从 UIImagePickerController 拍摄的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户使用 UIImagePickerController 从 iPhone 库中选择图像后,我想使用 ASIHTTPRequest 库将其上传到我的服务器.

After user choose image from the iPhone library with UIImagePickerController, I want to upload it to my server using ASIHTTPRequest library.

我知道使用 ASIHTTPRequest 我可以上传带有文件 URl 的文件,但是如何获取图像 URL?

I know that with ASIHTTPRequest I can upload a file with the file's URl, but how do I get the image URL?

我知道我可以得到图像 UIImagePickerControllerReferenceURL,看起来像这样:

I know I can get the image UIImagePickerControllerReferenceURL, that look like this:

"assets-library://asset/asset.JPG?id=F2829B2E-6C6B-4569-932E-7DB03FBF7763&ext=JPG"

这是我需要使用的网址吗?

is this the URL I need to use?

推荐答案

有很多答案建议使用 UIImageJPEGRepresentation 或 UIImagePNGRepresentation.但是这些解决方案会转换原始文件,而这个问题实际上是关于按原样保存文件.

There are lots of answers that suggest UIImageJPEGRepresentation or UIImagePNGRepresentation. But these solutions will convert the original file while this question is actually about saving the file AS IS.

看起来直接从资产库上传文件是不可能的.但是可以使用 PHImageManager 访问它以获取实际的图像数据.方法如下:

It looks like uploading the file directly from assets library is impossible. But it can be accessed using PHImageManager to get the actual image data. Here's how:

Swift 3(Xcode 8,仅适用于 iOS 8.0 及更高版本)

Swift 3 (Xcode 8, only for iOS 8.0 and higher)

1) 导入照片框架

import Photos

2) 在 imagePickerController(_:didFinishPickingMediaWithInfo:) 中获取资产 URL

2) In the imagePickerController(_:didFinishPickingMediaWithInfo:) get the asset URL

3) 使用 fetchAssets(withALAssetURLs:options:) 获取资源

3) Fetch assets using fetchAssets(withALAssetURLs:options:)

4) 使用 requestImageData(for:options:resultHandler:) 获取实际的图像数据.在此方法的结果处理程序中,您将拥有数据以及文件的 URL(可以在模拟器上访问 URL,但遗憾的是无法在设备上访问 - 在我的测试中 startAccessingSecurityScopedResource() 总是返回 false).不过这个 URL 对找出文件名还是很有用的.

4) Get the actual image data with requestImageData(for:options:resultHandler:). In the result handler of this method you will have the data as well as URL to the file (the URL can be accessed on Simulator, but unfortunately is not accessible on the device - in my tests startAccessingSecurityScopedResource() always returned false). However this URL is still useful to find out the file name.

示例代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    dismiss(animated: true, completion: nil)
    if let assetURL = info[UIImagePickerControllerReferenceURL] as? URL,
        let asset = PHAsset.fetchAssets(withALAssetURLs: [assetURL], options: nil).firstObject,
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first,
        let targetURL = Foundation.URL(string: "file://(documentsPath)") {

        PHImageManager.default().requestImageData(for: asset, options: nil, resultHandler: { (data, UTI, _, info) in
            if let imageURL = info?["PHImageFileURLKey"] as? URL,
                   imageData = data {
                    do {
                        try data.write(to: targetURL.appendingPathComponent(imageURL.lastPathComponent), options: .atomic)

                        self.proceedWithUploadFromPath(targetPath: targetURL.appendingPathComponent(imageURL.lastPathComponent))

                   } catch { print(error) }
                }
            }
        })
    }
}

这将为您提供包含正确名称的文件原样,您甚至可以获取其 UTI 以确定正确的 mimetype(除了通过文件扩展名确定它),同时准备多部分正文以供上传.

This will give you the file AS IS including its correct name and you can even get its UTI to determine correct mimetype (in addition to determining it via file extension) while preparing multipart body for upload.

这篇关于如何上传从 UIImagePickerController 拍摄的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆