gpt4 book ai didi

ios - Xcode - iOS - AWS S3 - 下载的图像在首次下载时不显示

转载 作者:行者123 更新时间:2023-11-29 01:52:31 24 4
gpt4 key购买 nike

这是我用来从 AWS S3 服务器下载图像并将其指定为 ImageView 中显示的图像的代码:

    let s3BucketName = "bucketName"
let fileName = Globals.currAuthorName.filter { $0 != Character(" ") } + ".jpg"

let downloadFilePath = NSTemporaryDirectory().stringByAppendingPathComponent(fileName)
let downloadingFileURL = NSURL.fileURLWithPath(downloadFilePath)

// Create a credential provider for AWS requests
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType: AWSRegionType.USEast1,
identityPoolId: "us-east-1:********-****-****-****-************")

// Create a service configuration for AWS requests
let defaultServiceConfiguration = AWSServiceConfiguration(
region: AWSRegionType.USEast1,
credentialsProvider: credentialsProvider)

// Create a new download request to S3, and set its properties
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration

let downloadRequest = AWSS3TransferManagerDownloadRequest()
downloadRequest.bucket = s3BucketName
downloadRequest.key = "folderName/" + fileName
downloadRequest.downloadingFileURL = downloadingFileURL

let transferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.download(downloadRequest)

// Set the UIImageView to show the file that was downloaded
let image = UIImage(contentsOfFile: downloadFilePath)
authorImage.contentMode = UIViewContentMode.ScaleAspectFit
authorImage.image = image

问题是,当我运行 iOS 模拟器并导航到相应的页面时,第一次访问该页面时图像永远不会加载。但是,当我离开页面然后返回页面时,它总是被加载,并且每次我访问该页面时。当我停止运行该应用程序然后再次启动它并再次访问该页面时,图像仍在加载。仅当我第一次访问某个 iOS 模拟器的页面时,图像才未加载(即在 iPhone 6 模拟器、iPhone 5 模拟器等中发生过一次)

我尝试通过添加这段代码来解决问题:

while (image == nil) {
transferManager.download(downloadRequest)
let image = UIImage(contentsOfFile: downloadFilePath)
}

但这只会导致我第一次访问该页面时出现无限循环。控制台最终也输出此错误:

2015-07-06 23:26:28.529 CCBF[19839:92323] 19839: CFNetwork internal error (0xc01a:/SourceCache/CFNetwork_Sim/CFNetwork-711.3.18/Foundation/NSURLRequest.mm:798) 2015-07-06 23:26:28.564 AuthorProject[19839:92323] AWSiOSSDKv2 [Error] AWSURLSessionManager.m line:240 | __41-[AWSURLSessionManager taskWithDelegate:]_block_invoke222 | Invalid AWSURLSessionTaskType.

编辑:

我尝试实现以下 continuewithblock:

var image : UIImage!

let task = transferManager.download(downloadRequest)
task.continueWithBlock({
if task.error != nil {
println(task.error)
} else {
dispatch_async(dispatch_get_main_queue(), {
image = UIImage(contentsOfFile: downloadFilePath)
self.authorImage.contentMode = UIViewContentMode.ScaleAspectFit
self.authorImage.image = image
})
}
return nil
}())

但是,当我运行它并尝试加载图像时,我收到一个运行时错误,该错误将我指向“continueWithExecutor”方法的代码。具体来说,代码行:

result = block(self);

消息是:

EXC_BAD_ACCESS(code: 1, address=0x10)

编辑 2:

我似乎找到了一个有效的解决方案:

var image : UIImage!

let taskTODO = transferManager.download(downloadRequest)
taskTODO.continueWithBlock{ (task: AWSTask!) -> AnyObject! in
if task.error != nil {
println(task.error)
} else {
dispatch_async(dispatch_get_main_queue(), {
image = UIImage(contentsOfFile: downloadFilePath)
self.authorImage.contentMode = UIViewContentMode.ScaleAspectFit
self.authorImage.image = image
})
}

return nil
}

这看起来是正确的方法吗?

最佳答案

transferManager.download(downloadRequest) 是异步的,这意味着下载图像大约需要 20 毫秒。但是,第一次进入屏幕时,您会尝试立即分配图像(尚未下载)。在随后的屏幕加载中——图像已经被下载和缓存——所以它可以立即显示,。您需要的是一个完成 block ,指示图像何时完成下载并准备好显示,您将在该 block 中分配图像。请参见下面的示例:

let readRequest1 : AWSS3TransferManagerDownloadRequest = AWSS3TransferManagerDownloadRequest()
readRequest1.bucket = "shrikar-picbucket"
readRequest1.key = "bingo"
readRequest1.downloadingFileURL = downloadingFileURL1

let task = transferManager.download(readRequest1)
task.continueWithBlock { (task) ->; AnyObject! in
println(task.error)
if task.error != nil {
} else {
dispatch_async(dispatch_get_main_queue()
, { () ->; Void in
self.selectedImage.image = UIImage(contentsOfFile: downloadingFilePath1)
self.selectedImage.setNeedsDisplay()
self.selectedImage.reloadInputViews()

})
println("Fetched image")
}
return nil
}

关于ios - Xcode - iOS - AWS S3 - 下载的图像在首次下载时不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31261297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com