Search code examples
iosuiviewcontrolleruiimageuistoryboardsegue

Passing Image of ImagePickerView in another View Controller


Hello to everyone in my view controller I'm using imagePickerView

When the user selects a photo from the library I would like the selected photo will be displayed in another view controller .

I'm currently using the storyboard so I'm using this method:

- (void) imagePickerController : ( UIImagePickerController *) picker didFinishPickingMediaWithInfo : ( NSDictionary *) info {
        
    UIImage * PhotoForEdit = self.FF_image ;
    PhotoForEdit = [info objectForKey : UIImagePickerControllerEditedImage ] ;
    
   FFEditingPhotoPost *VCPhotoEdit = [[FFEditingPhotoPost alloc] initWithImage:PhotoForEdit];
   [self performSegueWithIdentifier:@"immagine"sender:self];  
   [self.navigationController pushViewController:VCPhotoEdit animated:NO];
   [self dismissViewControllerAnimated:YES completion:nil];

}





  - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
        FFEditingPhotoPost * controller = (FFEditingPhotoPost *)segue.destinationViewController;
        controller.FFOriginalImage = FF_image;
    }

My problem is that I can not go with storyboard image selected in view CONTROLLR next ... Where am I doing wrong ?

Thank you all for the advice

EDIT : insert view controller that should show photo

#import "FFEditingPhotoPost.h"
#import "UIImage+Resize.h"

@interface FFEditingPhotoPost ()

@end

@implementation FFEditingPhotoPost
@synthesize FFScrollView;
@synthesize FFImageView;
@synthesize FFOriginalImage;
@synthesize SelectedFoto;
@synthesize SelectedFotoMiniature;



- (id)initWithImage:(UIImage *)image {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        if (!image) {
            return nil;
        }

        FFOriginalImage = image;
       // self.fileUploadBackgroundTaskId = UIBackgroundTaskInvalid;
       // self.photoPostBackgroundTaskId = UIBackgroundTaskInvalid;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    FFScrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    FFScrollView.delegate = self;
    FFScrollView.backgroundColor = [UIColor blackColor];
   // self.view = FFScrollView;

    FFImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0f, 42.0f, 280.0f, 280.0f)];
    [FFImageView setBackgroundColor:[UIColor blackColor]];
    [FFImageView setImage:FFOriginalImage];
    [FFImageView setContentMode:UIViewContentModeScaleAspectFit];
    FFImageView.layer.masksToBounds = NO;
    FFImageView.layer.shadowRadius = 3.0f;
    FFImageView.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
    FFImageView.layer.shadowOpacity = 0.5f;
    FFImageView.layer.shouldRasterize = YES;


    [FFScrollView setContentSize:CGSizeMake(FFScrollView.bounds.size.width, FFImageView.frame.origin.y + FFImageView.frame.size.height + FFImageView.frame.size.height)];

    [self shouldUploadImage:FFOriginalImage];

}
- (BOOL)shouldUploadImage:(UIImage *)anImage {
    UIImage *resizedImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(560.0f, 560.0f) interpolationQuality:kCGInterpolationHigh];

    UIImage *thumbnailImage = [anImage thumbnailImage:86.0f transparentBorder:0.0f cornerRadius:10.0f interpolationQuality:kCGInterpolationDefault];

    // JPEG to decrease file size and enable faster uploads & downloads
    NSData *imageData = UIImageJPEGRepresentation(resizedImage, 0.8f);
    NSData *thumbnailImageData = UIImagePNGRepresentation(thumbnailImage);

    if (!imageData || !thumbnailImageData) {
        return NO;
    }

    SelectedFoto = [PFFile fileWithData:imageData];
    SelectedFotoMiniature = [PFFile fileWithData:thumbnailImageData];
       [SelectedFoto saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"Photo uploaded successfully");
            [SelectedFotoMiniature saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    NSLog(@"Thumbnail uploaded successfully");
                }
            }];
        } else {
        }
    }];

    return YES;
}

Solution

  • I think PhotoForEdit = [info objectForKey : UIImagePickerControllerEditedImage ] ; Should be self.FF_image =

    EDIT -----

    There are several things wrong with your code. Firstly, you are creating 2 copies of the FFEditingPhotoPost view controller. Who knows which one actually ends up on top.

    If you have a storyboard segue set up, then change your image picker code to just perform the segue like this:

    - (void) imagePickerController : ( UIImagePickerController *) picker didFinishPickingMediaWithInfo : ( NSDictionary *) info {
    
        self.FF_image = [info objectForKey : UIImagePickerControllerEditedImage ] ;
    
       [self performSegueWithIdentifier:@"immagine"sender:self];
    
    }
    

    Your prepareForSegue code is fine. Notice I also switched around the code that sets the self.FF_image property as this was the wrong way around.

    Now in your FFEditingPhotoPost view controller, you don't need the initWithImage method, simple use the viewWillAppear method to set your image. The viewDidLoad method is called before your prepareForSegue which means FFOriginalImage is nil while you try to use it.

    So use viewWillAppear like this:

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [FFImageView setImage:FFOriginalImage];
    }