0

好吧,最近几天这让我发疯了。我希望这是一个简单的解决方法。我正在尝试访问我的 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:)];
   }
}

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

任何帮助将不胜感激。谢谢!

4

1 回答 1

0

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

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

于 2012-10-17T21:08:44.163 回答