没有这样的文件位置 - 正在将图像从 Swift 上传到 S3
No such file location - Uploading image from Swift to S3
我正在尝试将图像上传到 Amazon S3,但出现以下错误:
可选(错误域=NSCocoaErrorDomain 代码=260 "The file “asset.JPG” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/asset.JPG,NSUnderlyingError=0x7fb0d8eb3b00 {错误域=NSPOSIXErrorDomain 代码=2 "No such file or directory"}})
下面的代码是我获取图像或视频文件路径的地方——它存储在变量 'fileLocation':
中
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//let mediaType2 : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let mediaType : CFString = info[UIImagePickerControllerMediaType] as! CFString
//if video - save it
if mediaType == kUTTypeMovie {
let path = (info[UIImagePickerControllerMediaURL] as! NSURL).path
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path!) && saveVideoVar == true{
UISaveVideoAtPathToSavedPhotosAlbum(path!, self, "video:didFinishSavingWithError:contextInfo:", nil)
}
}
else{
let img : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let screenSize: CGRect = UIScreen.mainScreen().bounds
var multiplyNum = screenSize.width / img.size.width
//if image height is going to be more than 60% of the screen, resize width and height to ensure that it isn't greater than 60% while keeping the aspect ratio correct
if ((img.size.height*multiplyNum) > (screenSize.height*0.6)){
multiplyNum = screenSize.height*0.6 / img.size.height
imageViewWidthConstraint.constant = (multiplyNum*img.size.width)
imageViewHeightConstraint.constant = screenSize.height*0.6
}
else{
imageViewWidthConstraint.constant = screenSize.width
imageViewHeightConstraint.constant = (multiplyNum*img.size.height)
}
imageView.image = img
}
let fileLocation: NSURL = info["UIImagePickerControllerReferenceURL"] as! NSURL
nwyt.uploadS3(fileLocation)
self.dismissViewControllerAnimated(true, completion: {})
}
无论我选择哪张照片,它总是给出相同的错误,文件 "asset.JPG" 无法打开...”即使当我打印 fileLocation 时,路径比这更复杂,例如:"assets-library://asset/asset.JPG?id=EC41AC54-CEBD-49AA-A1FA-864370D103C0&ext=JPG"
nwyt.uploadS3() 的实现:
func uploadS3(fileBody : NSURL){
let uploadRequest: AWSS3TransferManagerUploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = "bx-video"
uploadRequest.key = "testObject.jpg"
uploadRequest.body = fileBody
print(fileBody)
let transferManager: AWSS3TransferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {(task: AWSTask) -> AnyObject! in
if task.error != nil {
print(task.error)
NSLog("Error uploading : " + uploadRequest.key!)
}
else {
NSLog("Upload completed : " + uploadRequest.key!)
}
return nil
})
}
AWSS3TransferManager
不支持ALAssetsLibrary
。您需要将文件复制到您的应用程序磁盘 space 并传递文件 NSURL
。以 this sample 为例。
解决方案如下:
if let img : UIImage = imageView.image! as UIImage{
let path = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("image.png")
let imageData: NSData = UIImagePNGRepresentation(img)!
imageData.writeToFile(path as String, atomically: true)
// once the image is saved we can use the path to create a local fileurl
let url:NSURL = NSURL(fileURLWithPath: path as String)
nwyt.uploadS3(url)
}
"assets-library://..." 无法直接访问。但是你可以使用 PHImageManager (iOS8+) to find the file in the assets library and copy it in your app's sandbox, then use the URL to the newly created file to upload or do whenever you want. Example code can be found in this answer to question How to upload image that was taken from UIImagePickerController.
请注意, 更简单,但有一个严重的缺点 - 图像将被转换为 PNG(如果您改用 UIImageJPEGRepresentation,则将转换为 JPEG)。 JPEG -> PNG 转换会增加尺寸,而 PNG -> JPEG 可能会降低质量。无论如何,这将不再是那个原始图像。
我在尝试从共享扩展上传照片时收到了同样的错误代码=260。此类问题的解决方案是不要尝试从其路径上传图像,而是上传带有文件名的数据。这对我有用 https://github.com/daltoniam/SwiftHTTP api.
上传功能如下:
上传(数据:数据,文件名:字符串,mimeType:字符串)
我正在尝试将图像上传到 Amazon S3,但出现以下错误:
可选(错误域=NSCocoaErrorDomain 代码=260 "The file “asset.JPG” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/asset.JPG,NSUnderlyingError=0x7fb0d8eb3b00 {错误域=NSPOSIXErrorDomain 代码=2 "No such file or directory"}})
下面的代码是我获取图像或视频文件路径的地方——它存储在变量 'fileLocation':
中func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
//let mediaType2 : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let mediaType : CFString = info[UIImagePickerControllerMediaType] as! CFString
//if video - save it
if mediaType == kUTTypeMovie {
let path = (info[UIImagePickerControllerMediaURL] as! NSURL).path
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path!) && saveVideoVar == true{
UISaveVideoAtPathToSavedPhotosAlbum(path!, self, "video:didFinishSavingWithError:contextInfo:", nil)
}
}
else{
let img : UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
let screenSize: CGRect = UIScreen.mainScreen().bounds
var multiplyNum = screenSize.width / img.size.width
//if image height is going to be more than 60% of the screen, resize width and height to ensure that it isn't greater than 60% while keeping the aspect ratio correct
if ((img.size.height*multiplyNum) > (screenSize.height*0.6)){
multiplyNum = screenSize.height*0.6 / img.size.height
imageViewWidthConstraint.constant = (multiplyNum*img.size.width)
imageViewHeightConstraint.constant = screenSize.height*0.6
}
else{
imageViewWidthConstraint.constant = screenSize.width
imageViewHeightConstraint.constant = (multiplyNum*img.size.height)
}
imageView.image = img
}
let fileLocation: NSURL = info["UIImagePickerControllerReferenceURL"] as! NSURL
nwyt.uploadS3(fileLocation)
self.dismissViewControllerAnimated(true, completion: {})
}
无论我选择哪张照片,它总是给出相同的错误,文件 "asset.JPG" 无法打开...”即使当我打印 fileLocation 时,路径比这更复杂,例如:"assets-library://asset/asset.JPG?id=EC41AC54-CEBD-49AA-A1FA-864370D103C0&ext=JPG"
nwyt.uploadS3() 的实现:
func uploadS3(fileBody : NSURL){
let uploadRequest: AWSS3TransferManagerUploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.bucket = "bx-video"
uploadRequest.key = "testObject.jpg"
uploadRequest.body = fileBody
print(fileBody)
let transferManager: AWSS3TransferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: {(task: AWSTask) -> AnyObject! in
if task.error != nil {
print(task.error)
NSLog("Error uploading : " + uploadRequest.key!)
}
else {
NSLog("Upload completed : " + uploadRequest.key!)
}
return nil
})
}
AWSS3TransferManager
不支持ALAssetsLibrary
。您需要将文件复制到您的应用程序磁盘 space 并传递文件 NSURL
。以 this sample 为例。
解决方案如下:
if let img : UIImage = imageView.image! as UIImage{
let path = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("image.png")
let imageData: NSData = UIImagePNGRepresentation(img)!
imageData.writeToFile(path as String, atomically: true)
// once the image is saved we can use the path to create a local fileurl
let url:NSURL = NSURL(fileURLWithPath: path as String)
nwyt.uploadS3(url)
}
"assets-library://..." 无法直接访问。但是你可以使用 PHImageManager (iOS8+) to find the file in the assets library and copy it in your app's sandbox, then use the URL to the newly created file to upload or do whenever you want. Example code can be found in this answer to question How to upload image that was taken from UIImagePickerController.
请注意,
我在尝试从共享扩展上传照片时收到了同样的错误代码=260。此类问题的解决方案是不要尝试从其路径上传图像,而是上传带有文件名的数据。这对我有用 https://github.com/daltoniam/SwiftHTTP api.
上传功能如下: 上传(数据:数据,文件名:字符串,mimeType:字符串)