[iOS]从照片库中选择图像

时间:2013-04-05 09:47:33

标签: uiimageview gallery photo

我知道如何打开照片库,现在我想选择其中一个图像并将其放在UIImageView上,有什么方法可以做到吗?

1 个答案:

答案 0 :(得分:0)

你提到照片库意味着什么?

下面一个是从照片库访问图像的代码

-(void)photoLibraryAction
{
    NSLog(@"image");
    if(picker!=nil){
        [picker dismissModalViewControllerAnimated:YES];
        [test dismissPopoverAnimated:YES];
        picker.delegate=nil;
        picker=nil;
        test.delegate=nil;
        test=nil;
    }
    picker= [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    picker.delegate=(id)self;

    NSLog(@"%@",picker);
    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
    {
        [self  presentModalViewController:picker animated:YES];
    }
    // [self pre
    else if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
    {
        UIPopoverController *popImageLibrary=[[UIPopoverController alloc]initWithContentViewController:picker];
        test=popImageLibrary;
        test.delegate=(id)self;

        [test presentPopoverFromRect:CGRectMake(240, 730, 400, 0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    }

}

并设置图像选择器的代理

picker.delegate=(id)self;

然后使用委托方法

- (void)imagePickerController:(UIImagePickerController *)albumPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
     if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone || albumPicker.sourceType==UIImagePickerControllerSourceTypeCamera)
        {
            self.imageData=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
                [albumPicker dismissModalViewControllerAnimated:YES];
         }
        else
        {
            yourImageView.image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
            [test dismissPopoverAnimated:YES];            

         }

}