访问/参考imageView.image以便以后替换它

时间:2012-10-17 20:38:37

标签: iphone objective-c xcode ipad

好吧,这让我在过去几天疯了。我希望这是一个简单的解决方案。我正在尝试访问我的UIImageView.image,以便稍后可以替换它。

这里只需要代码:

每次用户点击图书馆中的照片时,我都会首先创建一个imageView。

// .h
@property (strong,nonatomic) UIImage *doneImage;
@property (strong,nonatomic) UIImageView *editImage;

// .m
@synthesize doneImage;
@synthesize editImage;

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xCoordinate, yCoordinate, widthOfImage, heightOfImage)];
imgView.image = originalImage;
imgView.userInteractionEnabled = YES;
[self.mainView addSubview:imgView];

// I add gestures
longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1;
longPress.numberOfTouchesRequired = 1;
[imgView addGestureRecognizer:longPress];
}

当有人点击其中一张图片时,会弹出

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {

editImage = (UIImageView*)recognizer.view; // I declare what editImage is and now have access to what imageView is being interacted with.
doneImage = editImage.image; // I then get the image of that for later use.

if(UIGestureRecognizerStateBegan == recognizer.state) {

        popoverEditor = [[UIPopoverController alloc] initWithContentViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"popupEditor"]];
[popoverEditor presentPopoverFromRect:editImage.bounds inView:editImage permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}

这会弹出一个带有4个按钮的菜单。然后我有一个IBAction尝试启动编辑部分。

- (IBAction)effectsEditorButton:(id)sender { 

// editImage is my imageView

if (editImage == nil) {
    NSLog(@"imageView was nil wtf");
} else {
    [self performSelector:@selector(editImageWithEffects:)];
   }
}

我的图片视图始终为零。我需要以某种方式获得对它的引用。我已经尝试过标签,但是一旦达到这一点,我似乎无法让它们保持价值。

任何帮助都会得到非常赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

您是否遗漏了代码?根据你在这里的内容,editImage将永远为null,因为它没有在任何地方声明(也许是Interface Builder中我们看不到的东西?)。

您需要一种从弹出窗口链接回视图控制器的方法。流行的方法是从弹出控制器返回时使用完成块,或者将“主”VC设置为弹出窗口中的弱属性。