栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Swift-AWS S3从照片库上传图像并下载

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swift-AWS S3从照片库上传图像并下载

经过大量研究后,我开始了这项工作,

import AWSS3import AWSCore

上载:

我认为您已经实施

UIImagePickerControllerDelegate
了。

第1步:

  • 创建用于保存网址的变量:

    var imageURL = NSURL()
  • 创建上传完成处理程序obj:

    var uploadCompletionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?

步骤2: 从中获取图片网址

imagePickerController(_:didFinishPickingMediaWithInfo:)

  • imageURL
    在此委托方法中设置的值:
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){//getting details of imagelet uploadFileURL = info[UIImagePickerControllerReferenceURL] as! NSURLlet imageName = uploadFileURL.lastPathComponentlet documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .UserDomainMask, true).first! as String// getting local pathlet localPath = (documentDirectory as NSString).stringByAppendingPathComponent(imageName!)//getting actual imagelet image = info[UIImagePickerControllerOriginalImage] as! UIImagelet data = UIImagePNGRepresentation(image)data!.writeToFile(localPath, atomically: true)let imageData = NSData(contentsOfFile: localPath)!imageURL = NSURL(fileURLWithPath: localPath)picker.dismissViewControllerAnimated(true, completion: nil)

    }

步骤3: 设置为“将图像上传到存储桶”

uploadImage
后,调用此方法
imageURL

func uploadImage(){    //defining bucket and upload file name    let S3BucketName: String = "bucketName"    let S3UploadKeyName: String = "public/testImage.jpg"    let expression = AWSS3TransferUtilityUploadexpression()    expression.uploadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in        dispatch_async(dispatch_get_main_queue(), { let progress = Float(totalBytesSent) / Float(totalBytesExpectedToSend) print("Progress is: (progress)")        })    }    self.uploadCompletionHandler = { (task, error) -> Void in        dispatch_async(dispatch_get_main_queue(), { if ((error) != nil){     print("Failed with error")     print("Error: (error!)"); } else{     print("Sucess") }        })    }    let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()    transferUtility.uploadFile(imageURL, bucket: S3BucketName, key: S3UploadKeyName, contentType: "image/jpeg", expression: expression, completionHander: uploadCompletionHandler).continueWithBlock { (task) -> AnyObject! in        if let error = task.error { print("Error: (error.localizedDescription)")        }        if let exception = task.exception { print("Exception: (exception.description)")        }        if let _ = task.result { print("Upload Starting!")        }        return nil;    }}

下载:

func downloadImage(){    var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?    let S3BucketName: String = "bucketName"    let S3DownloadKeyName: String = "public/testImage.jpg"    let expression = AWSS3TransferUtilityDownloadexpression()    expression.downloadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in        dispatch_async(dispatch_get_main_queue(), { let progress = Float(totalBytesSent) / Float(totalBytesExpectedToSend) print("Progress is: (progress)")        })    }    completionHandler = { (task, location, data, error) -> Void in        dispatch_async(dispatch_get_main_queue(), { if ((error) != nil){     print("Failed with error")     print("Error: (error!)") } else{     //Set your image     var downloadedImage = UIImage(data: data!) }        })    }    let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()    transferUtility.downloadToURL(nil, bucket: S3BucketName, key: S3DownloadKeyName, expression: expression, completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in        if let error = task.error { print("Error: (error.localizedDescription)")        }        if let exception = task.exception { print("Exception: (exception.description)")        }        if let _ = task.result { print("Download Starting!")        }        return nil;    }}

参考 用于上传图片

参考 下载图片

非常感谢jzorz



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614014.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号