Ios 通过在图像视图中显示的代码访问图像库

Ios 通过在图像视图中显示的代码访问图像库,ios,gallery,uistepper,Ios,Gallery,Uistepper,我刚刚开始了一个新项目,我希望用户能够在设备库中选择一个图像 我试图通过使用ImageView和UIStepper来实现这一点 我想将图库中的所有图像写入一个数组,并让imageView使用步进器的+和-按钮在数组中导航(根据单击选择当前数组位置+1或-1)。如果需要特定的图像组,请选择目录 NSMutableArray *result = [NSMutableArray array]; [[[NSBundle mainBundle] pathsForResourcesOfType:@"png

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

我试图通过使用
ImageView
UIStepper
来实现这一点


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

如果需要特定的图像组,请选择目录

NSMutableArray *result = [NSMutableArray array];
 [[[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
    NSString *path = [obj lastPathComponent];
    [result addObject:path];
];

如果您想要一组特定的图像,可以尝试这样的方法,选择目录

NSMutableArray *result = [NSMutableArray array];
 [[[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:nil] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
    NSString *path = [obj lastPathComponent];
    [result addObject:path];
];

我想,你只需要从你的设备中检索相机镜头的照片

如果是,请尝试以下方法:

阿拉塞斯特图书馆

//获取相机滚动图像

-(void)updateLastPhotoThumbnail{
AlassetLibrary*AssetLibrary=[[AlassetLibrary alloc]init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup*组,布尔*停止){
NSInteger numberOfAssets=[集团numberOfAssets];
如果(numberOfAssets>0){
NSLog(@“numberOfPictures:%d”,numberOfAssets);
//NSInteger lastIndex=资产数量-1;
int i=0;

对于(i=0;i我认为,您只需要从您的设备检索相机卷的照片

如果是,请尝试以下方法:

阿拉塞斯特图书馆

//获取相机滚动图像

-(void)updateLastPhotoThumbnail{
AlassetLibrary*AssetLibrary=[[AlassetLibrary alloc]init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup*组,布尔*停止){
NSInteger numberOfAssets=[集团numberOfAssets];
如果(numberOfAssets>0){
NSLog(@“numberOfPictures:%d”,numberOfAssets);
//NSInteger lastIndex=资产数量-1;
int i=0;

对于(i=0;i@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中n如何枚举访问全屏照片的设备库:

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中显示,如前所述ion,这是项目:


尚未进行广泛的测试,尽管在模拟器和真实设备上运行正常,但根据前面的讨论,项目如下:



还没有做过广泛的测试,虽然在模拟器和真实设备上都运行正常

没有直接访问设备库的选项吗?我不确定,这里有另一种方法:我没有将文件作为资源添加到项目中,我想从设备库中读取它们。嗯,看起来你应该看看AssetLibrary框架:。下面是一个代码示例:。我已经添加了框架,但仍然无法装箱AlasSetLibrary的实例。没有直接访问设备库的选项吗?我不确定,这里有另一种方法:我没有将文件作为资源添加到项目中,我想从设备库中读取它们。嗯,看起来你需要我们来看一下AssetLibrary框架:。下面是一个代码示例:。我已经添加了框架,但仍然无法装箱AlasSetLibrary的实例。我尝试使用NSMutableArray,但如果使用addObject方法,它将返回Null。你能提供一个更简单的代码示例来检索图像吗?@Exothug,上面的已经是最简单的code.如果您可以将所有库映像存储到一个数组中,请将
//在此处处理映像
替换为
dispatch\u async(dispatch\u get\u main\u queue(),^{[self.youImagesArray addObject:image];})
我无法使数组正常工作。如果我尝试记录数组,它将返回(null)@Exothug,好吧,除非你早点弄明白,否则我今天晚些时候会考虑做一个快速工作的GitHub项目。AlassetLibrary API还可以,但实际上并不是最友好的,记得在弄明白之前花了几天时间:)我尝试使用NSMutableArray,但是如果我使用addObject方法,它会返回null。您能提供一个更简单的代码示例来检索图像吗?@Exothug,上面的代码已经是最简单的代码了。如果您可以将所有库图像存储到一个数组中,请使用
dispatch\u async替换
//在此处处理图像
(dispatch_get_main_queue(),^{[self.youImagesArray addObject:image];})
我无法让数组工作。如果我尝试记录数组,它将返回(null)@Exothug,好吧,除非你早点弄明白,否则我今天晚些时候会考虑做一个快速工作的GitHub项目。AlassetLibrary API还可以,但实际上并不是最友好的,记得在弄明白之前花了几天时间:)