1

我知道如何打开照片库,现在我想选择其中一张图片并将其放在 UIImageView 上,有什么办法吗?

4

1 回答 1

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];            

         }

}
于 2013-04-08T11:36:34.843 回答