【问题标题】:How to pass an uiimage to another view controller?如何将 uiimage 传递给另一个视图控制器?
【发布时间】:2012-11-13 02:40:01
【问题描述】:

如何将MainViewController中uiimagepickercontroller中选中的uiimage传递给SecondViewController?它确实推送到了 SecondViewController,但是 uiimage 是空的。

我在网上搜索过,尝试了解决方案,但仍然无法正常工作。

photoImage 是我在 SecondViewController 中声明的uiimageview

- (void)imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   [picker dismissModalViewControllerAnimated:YES];

    SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [secondVC.photoImage setImage:[info objectForKey:@"UIImagePickerControllerEditedImage"]];
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
}

【问题讨论】:

  • [secondVC release] 不是问题所在,因为您在将其推送到导航控制器的堆栈后立即释放它?
  • 我去掉[secondVC release];还是一样,实在想不通
  • photoImage 的属性声明是什么样的?如果您在调用setImage: 之前输入NSLog(@"Image: %@", [info objectForKey:@"UIImagePickerControllerEditedImage"]);,您会得到什么?
  • @psycho 情况并非如此,通过将 viewController 推到导航堆栈上,navigationController 会保留它,因此它是安全的,并且需要 release
  • @zeropt7 执行@phillip Mills 提到的检查,并注意您应该使用常量UIImagePickerControllerEditedImage,而不是使用字符串@"UIImagePickerControllerEditedImage"。它们具有相同的值,但 Apple 可能会决定在以后更改此值。使用常量还将确保编译器检查常量的拼写以保存字符串中的拼写错误

标签: objective-c cocoa-touch uiviewcontroller uiimage uiimagepickercontroller


【解决方案1】:

创建一个共享类来存储图像,并在任何你想要的地方访问它。

在 MainViewController 中将该图像分配到该共享类的 ivar 中,然后在 SecondViewController 中访问它。

编辑:

下面的代码展示了如何实现共享类,这可能在语法上不正确,因为目前我没有使用mac,所以我无法编译。

@interface SomeManager : NSObject
    +(id)myImage;
@end

@implementation SomeManager
    +(id)myImage{    
        static id myImage= nil; 
        @synchronized([self class]){ 
            if (myImage== nil) { 
                myImage= //put your image here; 
            } 
        }
        return myImage;
    }
 @end

【讨论】:

  • 您有一些代码或教程链接可以为我指明正确的方向吗?非常感谢。
  • @zeropt7 — 不要这样做。你原来的方法是正确的。
【解决方案2】:

在调用该控制器的 viewDidLoad 方法之前,第二个视图控制器中的 UIImageView 不存在。将一个属性放入包含UIImageSecondViewController 中,在推送视图控制器(而不是尝试设置图像视图)时存储对要在其中使用的图像的引用,然后移动您的setImage:调用第二个视图控制器的`viewDidLoad`。

【讨论】:

    猜你喜欢
    • 2012-05-02
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多