Ios 为什么imageData在swift 3中返回零?
嗨,我想将图像转换为数据,但问题是数据将返回零 我想用Alamofire上传图像,所以我需要在使用Alamofire上传之前转换图像。这里是我的代码Ios 为什么imageData在swift 3中返回零?,ios,swift3,upload,nsdata,Ios,Swift3,Upload,Nsdata,嗨,我想将图像转换为数据,但问题是数据将返回零 我想用Alamofire上传图像,所以我需要在使用Alamofire上传之前转换图像。这里是我的代码 let imageData = UIImageJPEGRepresentation(ViewController.imageUpload[i], 1.0) Alamofire.upload(imageData! , to: "http://example.com/api/file?api_token=\(api_toke
let imageData = UIImageJPEGRepresentation(ViewController.imageUpload[i], 1.0)
Alamofire.upload(imageData! , to: "http://example.com/api/file?api_token=\(api_token)&id=\(postID)").responseJSON { response in
debugPrint(response)
}
尝试下面的代码来获取imageData,并确保您传递的是正确的图像,而不是零 巴布亚新几内亚:-
let data = UIImagePNGRepresentation(ViewController.imageUpload[i]) as NSData?
对于JPEG:-
let data = UIImageJPEGRepresentation(ViewController.imageUpload[i], 1.0) as NSData?
并检查保存时使用的图像扩展名(PNG或JPEG)
要了解图像扩展,请查看以下代码
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if (!(picker.sourceType == UIImagePickerControllerSourceType.Camera)) {
let assetPath = info[UIImagePickerControllerReferenceURL] as! NSURL
if assetPath.absoluteString.hasSuffix("JPG") {
}
}
}
不确定这里的ViewController是什么。打印ViewController.imageUpload[i]并检查阵列中是否有图像。如果imageUpload是局部变量,请尝试使用self.imageUpload。这些不是局部变量-我已计算并打印了它们显示imageUpload的打印值以及如何将图像添加到imageUpload数组?,{40,60}如何将图像添加到imageUpload数组中?问题是这些图像已由gallery中的用户选择,我只是将它们复制到该数组中,因此我不知道它们的名称如何检测图像是png或JPG?请检查didFinishPickingMediaWithInfo函数中的assetPath.absoluteString.Hasuffix,您能告诉我一些更简单的方法吗?因为我将这些图像复制到了静态数组中,正如您所看到的,并且imagePickerController位于另一个视图控制器中,请告诉我检测ViewController的方式。imageUpload[I]是“JPG”或“PNG”,现在我们忘了了解扩展。首先检查这两个代码,看看是否有imageData。