简体   繁体   English

Swift等效于CGImage的UIImage吗?

[英]Swift equivalent for UIImage with CGImage?

What is the Swift equivalent for the following expression? 以下表达式的Swift等效项是什么?

@property (strong, nonatomic) NSMutableArray *assets;

cell.imageView.image = [UIImage imageWithCGImage:[[self.assets objectAtIndex:indexPath.row] thumbnail]];

I tried: 我试过了:

private var assets: [AnyObject]

cell.imageView.image = UIImage(CGImage: self.assets[indexPath.row].thumbnail())

But this gives the error: 但这给出了错误:

Missing argument for parameter 'inBundle' in call

Edit: self.assets has objects from the following expression: 编辑: self.assets具有来自以下表达式的对象:

ALAssetsGroupEnumerationResultsBlock assetsEnumerationBlock = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

        if (result) {
            [self.assets insertObject:result atIndex:0];
        }

    };

Declare the property like this: 像这样声明属性:

var assets: [ALAsset]

And then, in the actual code of some method: 然后,在某些方法的实际代码中:

let thumb = self.assets[indexPath.row].thumbnail().takeUnretainedValue()
cell.imageView.image = UIImage(CGImage:thumb)

In the property declaration, I'm revealing to the compiler what this is really an array of. 在属性声明中,我向编译器说明了这实际上是一个数组。 Otherwise, it has no idea that thumbnail() is a real method or what sort of thing it returns. 否则,不知道thumbnail()是一个真正的方法,也不知道它返回的是哪种类型的东西。

In the first line of the actual code, I'm dealing with the fact that AssetLibrary framework doesn't have memory management info; 在实际代码的第一行中,我要处理的事实是AssetLibrary框架没有内存管理信息。 you can't proceed in Swift unless you resolve this somehow. 除非您以某种方式解决此问题,否则您无法在Swift中继续进行。

So, by the next line we've got an actual CGImage and can just proceed to use it. 因此,到下一行,我们有了一个实际的CGImage,可以继续使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM