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

时间:2011-12-13 05:30:23

标签: iphone objective-c ios4 uiimagepickercontroller

在我的应用程序中,我使用了UiimagepickerController来获取Video.in我的程序之间收到任何属于我的应用程序的Web服务, 我必须停止视频捕捉并保存视频。

我使用StopVideoCapture来做上面的事情,但它不会调用委托 - “

  

(void)imagePickerController:(UIImagePickerController *)选取器   didFinishPickingMediaWithInfo:(NSDictionary *)info

How to force call above delegate ??.or How to handle interruption Handling in UIImagePickerController` ..任何想法?

1 个答案:

答案 0 :(得分:2)

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

所以我不会考虑自己调用委托方法。但是,如果您向UIImagePickerViewController提供模式对话(我猜这种选择器很常见),那么您可以在委托方法之外将其关闭:

[[yourPicker parentViewController] dismissModalViewControllerAnimated:YES];

Source

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

#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类转换为我自己的模型类。

- (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 Documentation