没有这样的文件位置 - 将图像从Swift上传到S3

时间:2016-02-03 00:58:25

标签: ios swift amazon-web-services amazon-s3 uiimagepickercontroller

我正在尝试将图片上传到Amazon S3,但我收到以下错误:

可选(错误域= NSCocoaErrorDomain代码= 260“无法打开文件”asset.JPG“,因为没有此类文件。”UserInfo = {NSFilePath = / asset.JPG,NSUnderlyingError = 0x7fb0d8eb3b00 {Error Domain = NSPOSIXErrorDomain代码= 2“没有这样的文件或目录”}}})

以下代码是我获取图像或视频文件路径的位置 - 它存储在变量'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”无法打开......“即使我打印文件位置时,路径也比这复杂,例如:“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
    })
}

4 个答案:

答案 0 :(得分:3)

以下解决方案:

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)

            }

答案 1 :(得分:1)

AWSS3TransferManager不支持ALAssetsLibrary。您需要将文件复制到您的应用磁盘空间并传递文件NSURL。请参阅this sample作为示例。

答案 2 :(得分:0)

“assets-library:// ...”无法直接访问。但您可以使用PHImageManager(iOS8 +)在资源库中查找文件并将其复制到应用程序的沙箱中,然后使用URL将新创建的文件上载或随时执行。可以在this answer中找到示例代码,以问How to upload image that was taken from UIImagePickerController

请注意,solution by @Mitchell更简单,但有一个严重的缺点 - 图像将转换为PNG(如果使用UIImageJPEGRepresentation,则转换为JPEG)。 JPEG - > PNG转换将增加尺寸和PNG - > JPEG可能会失去质量。在任何情况下,这都不再是原始图像。

答案 3 :(得分:0)

我在尝试从共享扩展程序上传照片时收到了相同的错误代码= 260。针对此类问题的解决方案是不要尝试从其路径上传图像,而是使用文件名上传数据。这对我来说很有用https://github.com/daltoniam/SwiftHTTP api。

下载上传功能: 上传(数据:数据,文件名:字符串,mimeType:字符串)