setImage无法正常工作

时间:2011-11-21 22:43:45

标签: ios xcode

我在设置图片时遇到问题。我有一个代表告诉我何时从imagePickerController中获取图像。它将它发送给所有者控制器,然后我尝试在另一个控制器上设置图像。这一切看起来都应该有效......但每当我看到视图时,图像就不存在了。这是代码:

// this gets called when an image has been chosen from the library or taken from the camera
// it is in the viewController in charge of grabbing an image (.m file)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    if(self.delegate)
    {
        [self.delegate didAcquirePicture:image];
    }
}


//This is in the controller .m file
- (void)didAcquirePicture:(UIImage *)picture 
{
    if(picture != Nil)
    {
        self.photoEditView = [[PhotoEditViewController alloc] init];
        [self.photoPicker.imagePickerController dismissModalViewControllerAnimated:NO];
        [self.photoEditView.imageView setImage:picture];
        [self presentModalViewController: self.photoEditView animated:NO];

    }
}

//this is the photoEditViewController .h
#import <UIKit/UIKit.h>

@class PhotoEditViewController;

@protocol PhotoEditViewControllerDelegate
@end


@interface PhotoEditViewController : UIViewController {
    IBOutlet UIImageView *imageView;
}

@property (retain) UIImageView *imageView;


@end

//this is the photoEditView .m file
#import "PhotoEditViewController.h"

@implementation PhotoEditViewController

@synthesize imageView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
    [imageView dealloc];
}

@end

2 个答案:

答案 0 :(得分:1)

在将imagePickerController分配给imageView之前,看起来像是在释放imagePickerController时可能会释放图像。如果是这样,您可以使用

进行修复

[[picture retain] autorelease];

如上所述,针对nil而不是Nil进行测试。此外,最好使用[self.delegate respondsToSelector: @selector(didAcquirePicture)]; - 请记住向nil发送邮件是安全的。

答案 1 :(得分:1)

解决问题


事实证明,在完全加载视图并将控制权交还给事件循环之前,我的图像已被释放。这是一个时间问题,我没有看到它是愚蠢的。所以,我在我的.h文件中创建了一个名为tempImage的UIImage变量,然后我把它放在 - (void)viewDidLoad:

self.imageView.image = tempImage;

这就是我的didAcquirePicture方法现在的样子:

- (void)didAcquirePicture:(UIImage *)picture 
{
    if(picture != nil)
    {
        self.photoEditView = [[[PhotoEditViewController alloc] init] autorelease];

        self.photoEditView.tempImage = picture;

        [self.photoPicker.imagePickerController dismissModalViewControllerAnimated:NO];
        [self presentModalViewController: self.photoEditView animated:NO];
        [self.photoEditView setupStache];
    }
}

我基本上只是保存图像,然后当我知道视图加载时,我在viewDidLoad中设置图像。感谢大家帮助我做出最终答案!