简体   繁体   中英

Swift equivalent for UIImage with CGImage?

What is the Swift equivalent for the following expression?

@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:

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.

In the first line of the actual code, I'm dealing with the fact that AssetLibrary framework doesn't have memory management info; you can't proceed in Swift unless you resolve this somehow.

So, by the next line we've got an actual CGImage and can just proceed to use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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