从相机胶卷创建 UIImages 数组

发布于 2024-12-23 02:07:02 字数 154 浏览 2 评论 0原文

我想从相机胶卷中获取所有图像并从中创建 UIImage 数组。

我已经尝试弄清楚如何做到这一点大约一天了,但我一无所获。我似乎不知道如何从相机胶卷中检索项目。看来我见过的所有样本都枚举了所有相册。不过我可能是错的。

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

I would like to get all of the images from the camera roll and create an array of UIImages from them.

I have been trying to figure out how to do this for about a day now and I've gotten nowhere. I can't seem to figure out how to retrieve only items from the Camera Roll. It appears that all of the samples that I've seen all enumerate over all of the photo albums. I might be wrong about that though.

Any help would be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-12-30 02:07:02

您尝试过 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.");
                     }];    

那就抓住他们了。然后执行此操作来渲染它们(这是邪恶的黑客行为。您需要根据需要导入它们,而不是像这样一次性导入它们,否则您将遇到内存问题并崩溃)

-(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);


}

Have you tried ALAssetsLibrary? like this:

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.");
                     }];    

that'll nab them. then do this to render them (this is wicked hacky. you'll want to import them as needed and not all at once like this, or you'll run into memory issues and crash)

-(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);


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文