0

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];
        }

    };
Michael
  • 32,527
  • 49
  • 210
  • 370

1 Answers1

2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141