ios - 使用 AWS 的 Swift 显示 UIImageView

标签 ios swift amazon-s3 aws-sdk

我正在尝试使用 Swift 在 UIImageView 中渲染图像。源对象位于 AWS S3 存储桶中。我在谷歌上找不到任何示例代码。所以我尝试翻译此链接中给出的代码[ 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’

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

最佳答案

你试过这样吗

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

        })

关于ios - 使用 AWS 的 Swift 显示 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253166/

相关文章:

Swift MPRemoteCommandCenter nextTrackCommand 被调用 3 次

amazon-s3 - 将文件从在线共享点上传到 aws s3 存储桶

open-source - 内容管理系统和 S3

ios - UICollectionView 中仅某些单元格之间的自定义间距

ios - 按钮标题字体

ios - 当应用程序转到后台时,SwiftUI View 停止更新

java - 当对象没有公共(public)访问权限时,无法将对象放入 S3 存储桶中

ios - Crashlytics:dSYM已上载,但崩溃报告丢失

ios - Swift 将键路径作为函数的参数传递

ios - 在应用程序委托(delegate)之外使用解析函数时出错