在iPhone中显示Gallery中的图像

时间:2011-07-13 05:45:37

标签: iphone uiimageview

我是iPhone的新手,现在是学习阶段的一天。其实我想实现我读取存储在iPhone PHOTO Gallery中的图像,然后将其显示在我的应用程序中。

我在许多搜索引擎中搜索过但找不到任何东西。你们都是这里的专业人士。请引导我阅读一些代码或一些教程。

非常感谢

3 个答案:

答案 0 :(得分:11)

修改

同时查看此问题/答案以及用于从uiimagepickercontroller获取图像的方法,因为我之前提到的方法已被弃用。

didFinishPickingMediaWithInfo return nil photo


查看文档http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html并查看此链接

http://iphone.zcentric.com/2008/08/28/using-a-uiimagepickercontroller/

它有相同的示例。

您可以使用这些方法在UIImageView对象中获取图像

- (void)selectPhotos
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
    //Deprecated In IOS6[self presentModalViewController:picker animated:YES]; 
    [picker release];
}

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{
    imageView.image = image;
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

答案 1 :(得分:5)

您可以像这样访问iPhone图像库并从那里选择图像

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            if (picker == nil) {
                picker = [[UIImagePickerController alloc] init];
                picker.allowsEditing = NO;

            }
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            picker.delegate = self;
            // Make camera view full screen:
            picker.wantsFullScreenLayout = YES;
            [self.navigationController presentModalViewController:picker animated:YES];
        }

然后实现委托方法来获取图像......

- (void)imagePickerController:(UIImagePickerController *)picker1 didFinishPickingMediaWithInfo:(NSDictionary *)info
{ 

    cameraClickedImage=[info valueForKey:UIImagePickerControllerOriginalImage];
     UIImage *thumbImage = [cameraClickedImage imageByScalingAndCroppingForSize:CGSizeMake(320, 480)];
    clickedImageView.image =thumbImage;
    [picker1 dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker1 {
    NSLog(@"Cap1");
    [picker1 dismissModalViewControllerAnimated:YES];
    [self.navigationController popViewControllerAnimated:NO];
}

希望这会对你有帮助.......... 干杯......

答案 2 :(得分:0)

使用带有Xcode 4.6的iOS SDK 6.1,它可以通过以下方式为我工作。

首先,在 viewcontroller.h 文件中制作一个UIImagePickerControllerDelegate协议:

@interface MyViewController : UIViewController<UIImagePickerControllerDelegate>

然后实现以下方法。我有一个名为Choose的按钮:

- (IBAction)btnPicChooseTouched:(id)sender {
   UIImagePickerController *picker = [[UIImagePickerController alloc]init];
   picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   picker.delegate = self;
   [self presentModalViewController:picker animated:YES];
}

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    imgProfilePic.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [picker dismissModalViewControllerAnimated:YES];
}

-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissModalViewControllerAnimated:YES];
}