如何使用 Alamofire 从相机胶卷上传图像
How to upload image with Alamofire from camera roll
我在使用 Alamofire 上传图片时遇到问题。问题很简单——我不知道如何获取所选图像的 fileURL(NSURL)。
这是来自 Alamofire GitHub:
的简单代码
Alamofire.upload(
.POST,
"myCustomServerURL",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: "_formname".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"default")
//How to get fireURL?
multipartFormData.appendBodyPart(fileURL: imageURL, name: "unicorn")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
}
)
获取本地文件的 NSURL 的常用方法是什么?
您从选择器中获得了 UIImage
实例。获取图像并将其写入磁盘 als jpg (UIImageJPEGRepresentation) or png (UIImagePNGRepresentation) 并使用文件 URL 作为存储图像。
我在使用 Alamofire 上传图片时遇到问题。问题很简单——我不知道如何获取所选图像的 fileURL(NSURL)。 这是来自 Alamofire GitHub:
的简单代码Alamofire.upload(
.POST,
"myCustomServerURL",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: "_formname".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"default")
//How to get fireURL?
multipartFormData.appendBodyPart(fileURL: imageURL, name: "unicorn")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
}
)
获取本地文件的 NSURL 的常用方法是什么?
您从选择器中获得了 UIImage
实例。获取图像并将其写入磁盘 als jpg (UIImageJPEGRepresentation) or png (UIImagePNGRepresentation) 并使用文件 URL 作为存储图像。