I am trying to upload an image to Amazon S3 but I am getting the following error:
Optional(Error Domain=NSCocoaErrorDomain Code=260 "The file “asset.JPG” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/asset.JPG, NSUnderlyingError=0x7fb0d8eb3b00 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}})
The below code is where I am getting the image or videos file path - it is stored in the variable '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: {})
}
No matter which photo I chose, it always gives the same error that the file "asset.JPG" couldn't be opened..." even though when I print the fileLocation, the path is more complex than that, as an example: "assets-library://asset/asset.JPG?id=EC41AC54-CEBD-49AA-A1FA-864370D103C0&ext=JPG"
Implementation of 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
})
}
Solution below:
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)
}