Pergunta

I have written my own image picker classes through using ALAssetsLibrary classes.

Almost everything is fine, but there are some image thumbnails which have black background, while the actual image is transparent/alpha channel.

How to fix this issue?

Here is my enumeration block in which I've load the image from ALAsset thumbnail property:

[reversedItems enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
        UIImage *image = [UIImage imageWithCGImage:[[_assets objectAtIndex:allItems - idx] thumbnail]];

        dispatch_async(dispatch_get_main_queue(), ^{
            GridView *gridView = (GridView *)obj;
            gridView.imageView.image = image;
        });
    });

}];
Foi útil?

Solução

There is a workaround if you use the fullScreenImage property, it should be slower for execution but it should work fine.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
        ALAsset *asset = [_assets objectAtIndex:allItems - idx];
        UIImage *smallImage = [UIImage imageWithCGImage:[asset thumbnail]];
        UIImage *image;

        CGSize size = [smallImage size];
        CGRect box = CGRectMake(0, 0, size.width, size.height);


        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        CGContextFillRect(context, box);

        CGContextTranslateCTM(context, 0.0, size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        CGContextDrawImage(context, box, [[asset defaultRepresentation] fullScreenImage]);

        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();


        dispatch_async(dispatch_get_main_queue(), ^{
            TTGridView *gridView = (TTGridView *)obj;
            gridView.imageView.image = image;
        });
    });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top