我正在尝试使用 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/