ALAsset缩略图具有黑色背景

时间:2013-04-10 14:49:45

标签: objective-c alassetslibrary alasset

我已经使用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;
        });
    });

}];

1 个答案:

答案 0 :(得分: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;
        });
    });