从相机胶卷创建一个UIImages数组

时间:2011-12-29 05:05:15

标签: objective-c ios alassetslibrary

我想从相机胶卷中获取所有图像并从中创建一系列UIImages。

我一直试图弄清楚如何做这件事已经有一天了,而我却无处可去。我似乎无法弄清楚如何从相机胶卷中检索项目。我看到的所有样本似乎都列在所有相册上。不过我可能错了。

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:2)

您是否尝试过ALAssetsLibrary?像这样:

assets = [[NSMutableArray array] init]; // Prepare array to have retrieved images by Assets Library.

void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *asset, NSUInteger index, BOOL *stop) {
    if(asset != NULL) {
        [assets addObject:asset]; 
        dispatch_async(dispatch_get_main_queue(), ^{
            [self insertArray];
        });
    }
};

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
    if(group != nil) {
        [group enumerateAssetsUsingBlock:assetEnumerator];
    }
};

// Create instance of the Assets Library.
library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos // Retrieve the images saved in the Camera roll.
                       usingBlock:assetGroupEnumerator
                     failureBlock: ^(NSError *error) {
                         NSLog(@"Failed.");
                     }];    

那会抓住他们。然后这样做来渲染它们(这是邪恶的hacky。你会想要根据需要导入它们,而不是像这样一次导入它们,否则你会遇到内存问题并崩溃)

-(void) insertArray {

int i = assetCount++;

if (i>20) {
    return;
}

ALAssetRepresentation *rep = [[assets objectAtIndex:i] defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
CGSize cSize = CGSizeMake(75,75);
UIImage *largeimage = [UIImage imageWithCGImage:iref];
UIImage *resizedimage = (UIImage *)[largeimage resizedImage:cSize interpolationQuality:kCGInterpolationHigh];
UIImageView *newView = [[UIImageView alloc] initWithImage:resizedimage];
if((i>0)&&(i%4 == 0)){
    rowCount++;
}

colCount = i%4;
newView.frame = CGRectMake(4+(colCount*(75+4)), 4+(rowCount*(75+4)), 75, 75);

[sv addSubview:newView];
[sv setContentSize:CGSizeMake(320, 85+(rowCount*(75+4)))];

NSLog(@"sv frame size is %@ and i is %i", NSStringFromCGRect(sv.frame), i);


}