显示UIImageView的问题

时间:2012-08-20 19:15:38

标签: iphone ios xcode uiimageview

我正在尝试使用UIImageView显示[self.view addSubview:imageView];但是没有任何反应。 imageView不是空的。我使用以下代码从图像库中分配一个图像;

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info
                           objectForKey:UIImagePickerControllerMediaType];
    [self dismissModalViewControllerAnimated:YES];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info 
                          objectForKey:UIImagePickerControllerOriginalImage];

        [imageView setImage:image];
        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image, 
                                           self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
}

为什么不起作用?

imageView创建如下;

UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@synthesize imageView;

1 个答案:

答案 0 :(得分:1)

声明和合成属性imageView不会创建UIImageView的实例。因此,imageView在以下行中为nil

[imageView setImage:image];

在使用之前,您必须在某个时刻创建UIImageView的实例。例如:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //...
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    self.imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100)];
    self.imageView.image = image;
    [self.view addSubview:self.imageView];

    //...
}

或者,如果您正在使用它,可能忘记从Interface Builder链接到imageView