0

我正在尝试与 AWS SDK 集成,但图像未显示。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCollectionViewCell", forIndexPath: indexPath) as! MyCollectionViewCell

        let downloadURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("myImage.jpg")
        let downloadRequest = AWSS3TransferManagerDownloadRequest()
        downloadRequest.bucket = "stg"
        downloadRequest.key = "myImage.jpg"
        downloadRequest.downloadingFileURL = downloadURL

        let transferManager = AWSS3TransferManager.defaultS3TransferManager()
        transferManager.download(downloadRequest).continueWithBlock { (task) -> AnyObject! in
            if task.error != nil {
                print("Failed to download S3 with error \(task.error)")
            }

            if task.result != nil {
                //let output = task.result as! AWSS3TransferManagerDownloadOutput
                cell.imageView.contentMode = UIViewContentMode.ScaleAspectFit
                cell.imageView?.hnk_setImageFromURL(downloadURL)
            }
            return nil
        }
        return cell
    }
4

1 回答 1

0

问题是您正在后台线程中更新 UI。您应该替换.continueWithBlock { (task) -> AnyObject! in.continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task) -> AnyObject! in在主线程中运行该块。

此外,如果您正在编写一个新应用程序,我建议您使用AWSS3TransferUtility而不是,AWSS3TransferManager因为我们将把我们的开发工作重点放在传输实用程序上,并将逐步停止对传输管理器的支持。

于 2015-11-18T19:27:51.087 回答