我正在尝试使用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;
答案 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
。