2

我通过使用类编写了自己的图像选择器ALAssetsLibrary类。

几乎一切都很好,但是有些图像缩略图具有黑色背景,而实际图像是透明/Alpha 通道。

如何解决这个问题?

这是我从 ALAsset 缩略图属性加载图像的枚举块:

[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;
        });
    });

}];
4

1 回答 1

1

如果您使用 fullScreenImage 属性,则有一种解决方法,它的执行速度应该较慢,但应该可以正常工作。

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;
        });
    });
于 2013-04-26T11:24:54.377 回答