检查UIImage属性是否已分配?

时间:2012-06-11 07:36:48

标签: iphone objective-c ios cocoa-touch

我有UIImage属性,在从iPhone相机拍摄图像后,我将图像对象分配给此属性,否则没有。 如何在拍摄后检查此属性是否包含图像? 我的代码下面没有帮助。

@interface MyViewController : UIView<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property(nonatomic, retain) UIImage *imageShot;
@end

@implementation MyViewController
@synthesize imageShot = _imageShot;

- (id)init {
    self = [super init];
    if (self) {
        _imageShot = [[UiImage alloc] init];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    self.imageShot = [info objectForKey:UIImagePickerControllerEditedImage];
}

- (void)buttonClick {
    if (_imageShot) {

    } 
    else {

    }
}

@end

1 个答案:

答案 0 :(得分:6)

init中,您应该使用shotedImage创建nil,而不是分配新的空图像。这样就可以执行if (!shotedImage)nil == 0)等测试。