Display images from Gallery in iPhone

2019-03-31 11:53发布

问题:

I am new in iPhone and in learning phase now a days. Actually i want to implement that i read images stored in my iPhone PHOTO Gallery and then display it onto my application.

I have searched in many Search Engines but could not find any thing. You all are professionals over here. Please guide me thrrough some code or some tutorial.

Thanks alot

回答1:

Edit

Also check out this question/answer and the method that is used to get the image from uiimagepickercontroller as the method that i have mentioned earlier is deprecated.

didFinishPickingMediaWithInfo return nil photo


check out the documentation http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html and check out this link

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

It has sample examples about the same.

You can use these methods to get the image in you UIImageView object

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


回答2:

You can access the iPhone image library like this and select the image from there

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

And then implement the delegate method to get the image...

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

Hope this will help you.......... Cheers......



回答3:

It works for me in the following way using iOS SDK 6.1 with Xcode 4.6.

First, make an UIImagePickerControllerDelegate protocol like that in your viewcontroller.h file:

@interface MyViewController : UIViewController<UIImagePickerControllerDelegate>

Then implement the following methods. I have a button named 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];
}