使用AWS中的Swift显示UIImageView

时间:2015-08-27 15:11:45

标签: ios swift amazon-s3 aws-sdk

我尝试使用Swift在UIImageView中渲染图像。源对象位于AWS S3存储桶中。我在Google上找不到任何示例代码。所以我尝试将此链接[AWS S3 SDK v2 for iOS - Download an image file to UIImage中给出的代码从Obj-C转换为Swift,但失败了。我是iOS的初学者。

        let accessKey = "ACCESS_CODE";
        let secretKey = "SECRET_KEY";
        // let credentialsProvider = AWSStaticCredentialsProvider.credentialsWithAccessKey(accessKey, secretKey: secretKey) 
        // ^ Xcode says - credentialsWithAccessKey is deprecated, use initWithAccessKey 
        let credentialsProvider = AWSStaticCredentialsProvider.initWithAccessKey(accessKey, secretKey: secretKey)
        // ^ Xcode says - AWSStaticCredentialsProvider.Type does not have a member named ‘initWithAccessKey’

我可能在这里做了很多错事,甚至是愚蠢的错误。最好的帮助是指出一些示例代码。

2 个答案:

答案 0 :(得分:2)

你有没有试过这个

var credentialsProvider: AWSStaticCredentialsProvider = AWSStaticCredentialsProvider.credentialsWithAccessKey("MY_ACCESS_KEY", secretKey: "MY_SECRET_KEY")
var configuration: AWSServiceConfiguration = AWSServiceConfiguration.configurationWithRegion(AWSRegionUSWest1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
var transferManager: AWSS3 = AWSS3(configuration: configuration)
var getImageRequest: AWSS3GetObjectRequest = AWSS3GetObjectRequest.new()
getImageRequest.bucket = "MY_BUCKET"
getImageRequest.key = "MY_KEY"
transferManager.getObject(getImageRequest).continueWithExecutor(BFExecutor.mainThreadExecutor(), withBlock: {(task: BFTask) -> id in    if task.error {
        NSLog("Error: %@", task.error)
    }
    else {
        NSLog("Got image")
        var data: NSData = task.result.body()
        dispatch_async(dispatch_get_main_queue(), {    
        var image: UIImage = UIImage.imageWithData(data)
        myImageView.image = image

        })

答案 1 :(得分:0)

我创建了一个自定义类来下载图像。它对我有用。

class AWSImageDownloader {

    init(AccessKey accessKey:String, SecretKey secretKey:String, andRegion region:AWSRegionType = .USEast1) {

        let credentialsProvider = AWSStaticCredentialsProvider(accessKey: accessKey, secretKey: secretKey)
        guard let configuration = AWSServiceConfiguration(region: region, credentialsProvider: credentialsProvider) else {
            debugPrint("Failed to configure")
            return
        }

        AWSServiceManager.default().defaultServiceConfiguration = configuration
    }

    func downloadImage(Name imageName:String, fromBucket bucketName:String, onDownload successCallback:@escaping AWSImageDownloadSuccess, andOnError errorCallback:@escaping AWSImageDownloadError){

        let transferManager     = AWSS3.default()
        let getImageRequest     = AWSS3GetObjectRequest()
        getImageRequest?.bucket = bucketName
        getImageRequest?.key    = imageName
        transferManager.getObject(getImageRequest!).continueWith(executor: AWSExecutor.mainThread()) { (anandt) -> Void in
            if anandt.error == nil {
                if let imageData = anandt.result?.body as? Data, let image = UIImage(data: imageData) {

                    successCallback(image)
                } else {
                    errorCallback("Download failed")
                }
            } else {

                let error = "Error \(anandt.error?.localizedDescription ?? "unknown by dev")"
                errorCallback(error)
            }
        }
    }
}