Harish Harish - 8 months ago 40
iOS Question

Loading Image into ImageView by taking the Image from FilePath

- (IBAction)chooseImgBtnClick:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
_image=[info objectForKey:UIImagePickerControllerOriginalImage];
[_vehicleImageView setImage:_image];
[self dismissViewControllerAnimated:YES completion:NULL];_vehicleImageView.image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(_vehicleImageView.image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"png"];
[webData writeToFile:localFilePath atomically:YES];

Using the above code I am choosing a photo from the library and setting the photo in the UIImageView and tried to take a path into a string called localFilePath. I assigned the path to _filepath.

Now I wrote a function to take the image from the filepath and load the image into another UIImageView upon click of the another button.
But the image is not getting loaded. Please advise

Below is the function.

- (IBAction)loadSecondView:(id)sender {
NSData *pngData = [NSData dataWithContentsOfFile:_filePath];
UIImage *image = [UIImage imageWithData:pngData];
[_secondImageView setImage:image];



I did saved the image name into an attribute. After which I used the below code to take back the image from the particular path and put in the ImageView.

-(UIImage*) getImage:(NSString *) imageName{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      imageName ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    return image;