user3189333
user3189333

Reputation:

how to get image from photo library by giving a image name in iOS 7

i want to get image from photo library in iPhone simulator by simply giving image name or full path. but i cant get idea to do that. please get me out from here. Thank you in advance.

Upvotes: 1

Views: 1304

Answers (1)

Jekil Patel
Jekil Patel

Reputation: 403

To access library directory you need to used NSLibraryDirectory instead of NSDocumentDirectory and then use following code. I think it might be useful to you.

NSString* fileName = [NSString stringWithFormat:@"%@.png",patientlastName];
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES); 
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
UIImage *image1=[UIImage imageWithContentsOfFile:pdfFileName];
imageView.image=image1;

If you would like to open photo library and get image from there, you have to use UIImagePickerController and implements its delegate methods like below.

above the @implementation of your class in .m file, use folllowing code

#pragma mark - NON Rotation
@interface NonRotatingUIImagePickerController : UIImagePickerController

@end

@implementation NonRotatingUIImagePickerController

- (BOOL)shouldAutorotate
{
    return NO;
}

@end

then in implementation of your class .m file use below code.

-(void)openPhotoLibrary {

    if ([UIImagePickerController isSourceTypeAvailable:
                         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
                    {
                        UIImagePickerController *imagePicker =
                        [[NonRotatingUIImagePickerController alloc] init];
                        imagePicker.delegate = self;
                        imagePicker.sourceType =
                        UIImagePickerControllerSourceTypePhotoLibrary;
                        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                                  (NSString *) kUTTypeImage,
                                                  nil];
                        imagePicker.allowsEditing = NO;
    }
}

UIImagePickerController Delegates

#pragma mark - ImagePicker Controller Delegate
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
 NSString *mediaType = [info
                           objectForKey:UIImagePickerControllerMediaType];
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
                          objectForKey:UIImagePickerControllerOriginalImage];

        imageView.image = image;
}
}

-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
 contextInfo:(void *)contextInfo
{
    if (error) {
//Right some error related code...
}
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

I think this would be helpful to you.

Upvotes: 1

Related Questions