在另一个视图控制器中传递ImagePickerView的图像

时间:2013-12-06 15:32:07

标签: ios uiviewcontroller uiimage uistoryboardsegue

我的视图控制器中的每个人都好,我正在使用imagePickerView

当用户从库中选择照片时,我希望所选照片将显示在另一个视图控制器中。

我目前正在使用故事板,所以我正在使用这种方法:

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

我的问题是我无法使用在视图CONTROLLR中选择的故事板图像...我在哪里做错了?

谢谢大家的建议

编辑:插入应显示照片的视图控制器

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

1 个答案:

答案 0 :(得分:2)

我认为PhotoForEdit = [info objectForKey:UIImagePickerControllerEditedImage];应该是self.FF_image =

编辑-----

您的代码有几个问题。首先,您要创建FFEditingPhotoPost视图控制器的2个副本。谁知道哪一个实际上最终是最重要的。

如果你设置了故事板segue,那么改变你的图像选择器代码,就像这样执行segue:

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

    self.FF_image = [info objectForKey : UIImagePickerControllerEditedImage ] ;

   [self performSegueWithIdentifier:@"immagine"sender:self];

}

您的prepareForSegue代码没问题。注意我也切换了设置self.FF_image属性的代码,因为这是错误的方法。

现在,在FFEditingPhotoPost视图控制器中,您不需要initWithImage方法,只需使用viewWillAppear方法设置图片即可。在viewDidLoad之前调用prepareForSegue方法,这意味着当您尝试使用FFOriginalImage时,viewWillAppear为零。

所以像这样使用- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [FFImageView setImage:FFOriginalImage]; }

{{1}}