且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以编程方式或强制方式调用UIImagePickerController委托[无需用户交互]?

更新时间:2023-02-20 20:35:14

委托方法的想法不是你称之为他们叫你。

The idea with delegate methods is not that you call those methods - "They call you".

所以我不会考虑自己调用委托方法。但是,如果你使用模态对话(我猜这种选择器很常见)来呈现 UIImagePickerViewController ,那么你可以在委托方法之外关闭它:

So I would not consider calling the delegate method yourself a good practise. However, if you present the UIImagePickerViewController with a modal dialogue (which I guess is common for such a picker) then you can close it like this outside of your delegate method:

[[yourPicker parentViewController] dismissModalViewControllerAnimated:YES];

来源

更新:您可以使用 ALAssetsLibrary ,用于访问iPhone媒体库中存储的数据。我最近不得不做一个类似的项目,我必须列出iPhone上的所有图像。 Github项目 ELCImagePickerController.git 非常有用,因为它显示了如何访问库中的项目。所以你会做这样的事情:

Update: You can use the ALAssetsLibrary for accessing the stored data in your iPhone media library. I recently had to do a similar project where I had to list all images on the iPhone. The Github project ELCImagePickerController.git was very useful since it shows how the items in your library can be accessed. So you'll do something like this:

#import <AssetsLibrary/AssetsLibrary.h>

// ....

-(void)fetchPhotoAlbums{

    if(!self.assetsGroups){
        self.assetsGroups = [NSMutableDictionary dictionary];
    }

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];      
    NSMutableArray *returnArray = [[NSMutableArray alloc] init];

        @autoreleasepool {
            void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop){
                if (group == nil){
                   // Completed
                   [self.delegate pictureService:self fetchedAlbums:returnArray]; 
                    return;
                }

                Album *currentAlbum = [self albumForAssetsGroup:group];

                // Store the Group for later retrieving the pictures for the album
                [self.assetsGroups setObject:group forKey:currentAlbum.identifier];
                [returnArray addObject:currentAlbum];
                [self.delegate pictureService:self fetchedAlbums:returnArray];
             };

            void (^assetGroupEnumberatorFailure)(NSError *) = ^(NSError *error) {
                NSLog(@"A problem occured %@", [error description]);                                     
            };  

            [library enumerateGroupsWithTypes:ALAssetsGroupAll
                                   usingBlock:assetGroupEnumerator 
                                 failureBlock:assetGroupEnumberatorFailure];   
        }
}


-(void)fetchPhotosForAlbum:(Album *)album{

    ALAssetsGroup *currentGroup = [self.assetsGroups objectForKey:album.identifier];
    NSMutableArray *photos = [NSMutableArray array];
    [currentGroup enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop){         
         if(asset == nil){
             [self.delegate pictureService:self fetchedPictures:photos forAlbum:album];
             return;
         }

         [photos addObject:[self pictureForAsset:asset]];
     }];
}

此外,我使用两个映射器方法将AL类转换为我自己的模型class。

Additionally I use two mapper methods to convert the AL-classes into my own model classes.

- (Album *)albumForAssetsGroup:(ALAssetsGroup *)assetsGroup{
    Album *album = [[Album alloc] init];
    album.title = [assetsGroup valueForProperty:ALAssetsGroupPropertyName];
    album.identifier = [assetsGroup valueForProperty: ALAssetsGroupPropertyPersistentID];
    album.assetsCount = assetsGroup.numberOfAssets;
    album.thumbnail = [UIImage imageWithCGImage:assetsGroup.posterImage];
    return album;
}

- (Picture *)pictureForAsset:(ALAsset *)asset{
    Picture *picture = [[Picture alloc]init];
    picture.identifier = [((NSArray *)[asset valueForProperty: ALAssetPropertyRepresentations]) objectAtIndex:0];
    picture.thumbnail = [UIImage imageWithCGImage:asset.thumbnail];
    return picture;
}

参见 AssetsLibrary文档