0

我刚刚开始了一个新项目,我希望用户能够在设备库中选择其中一个图像。

我试图通过使用 anImageView和 a来实现这一点UIStepper

我想将画廊内的所有图像写入一个数组,并imageView使用步进器的 + 和 - 按钮在数组中导航(根据点击选择当前数组位置 +1 或 -1)。

4

4 回答 4

2

好的,根据之前的讨论,这里是项目:AssetLibraryPhotosViewer

尚未进行广泛的测试,但在模拟器和真实设备上似乎都运行良好

于 2013-07-18T14:58:08.203 回答
1

@Exothug,让您了解如何枚举访问全屏照片的设备库:

ALAssetsLibrary* assetLibrary = [[ALAssetsLibrary alloc] init];

[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    if (group) {
        [group enumerateAssetsUsingBlock:^(ALAsset* asset, NSUInteger index, BOOL* innerstop) {
             if (asset) {
                 ALAssetRepresentation *rep = [asset defaultRepresentation];
                 CGImageRef iref = [rep fullScreenImage];
                 if (iref) {
                     UIImage *image = [UIImage imageWithCGImage:iref scale:rep.scale
                                                    orientation:(UIImageOrientation)rep.orientation];
                     // process the image here
                 }
             }
         }];
    }
} failureBlock:^(NSError *error) {
    NSLog(@"failure: %@", [error localizedDescription]);
}];    

您可以通过将图像添加到数组来处理图像,但是根据库中图像的数量,它可能不是最有效的。另一种方法是使用图像 URL / 索引来遍历库,根据需要从库中获取图像以在 ImageView 中显示

于 2013-07-18T08:52:12.150 回答
0

也许尝试这样的事情,如果你想要一组特定的图像,请选择目录。

NSMutableArray *result = [NSMutableArray array];
 [[[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
    NSString *path = [obj lastPathComponent];
    [result addObject:path];
];
于 2013-07-18T08:17:27.707 回答
0

我以为,您只需要从您的设备中检索相机胶卷的照片。

如果是这样,试试这个:

ALA资产库

void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != NULL) {
            NSLog(@"See Asset: %@", result);
        }
    };

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

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock: ^(NSError *error) {
                             NSLog(@"Failure");
                         }];

或者

//获取相机胶卷图像

- (void)updateLastPhotoThumbnail {
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    NSInteger numberOfAssets = [group numberOfAssets];
    if (numberOfAssets > 0) {
        NSLog(@"numberOfPictures: %d",numberOfAssets);
        //NSInteger lastIndex = numberOfAssets - 1;
        int i = 0;
        for (i = 0; i <= numberOfAssets-1; i++)  {
            [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:i] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
                NSLog(@"theObject!!!! -- (%d) %@",i,thumbnail);
                [cameraRollPictures addObject:thumbnail];
            }];
        }
    }
} failureBlock:^(NSError *error) {
    NSLog(@"error: %@", error);
}];
于 2013-07-18T08:34:18.663 回答