1

很简单,我想用它拍照UIImagePicker并将其放入NSMutableArray. 我有以下代码。相机部分工作正常,出于测试目的,图像显示在UIImage名为photoImageView中。但是我似乎无法将图像添加到数组中。感激地收到任何帮助。

@property (nonatomic, retain) NSMutableArray *photos;

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *photoTaken = (UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];
photoImageView.image=photoTaken;
[photos addObject:[UIImage photoTaken];
[picker dismissViewControllerAnimated:YES completion:nil];
}
4

4 回答 4

1

您是否在某处实例化了数组(viewDidLoad例如):

self.photos = [NSMutableArray alloc] init];

?

于 2013-11-10T12:50:52.107 回答
1

1)你在初始化照片NSMutableArray吗?有时 addObject: 在尝试将对象插入 NULL NSArray 时不会抛出错误。

2)

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *photoTaken = (UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];
    if (photoTaken)
    {
        photoImageView.image=photoTaken;
        [photos addObject:photoTaken];
    }else{
        NSLog(@"Selected photo is NULL");
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}
于 2013-11-10T12:51:51.367 回答
1

添加到 (Ian L ) 后在下面修改

     [photos addObject: photoTaken];
于 2013-11-10T12:54:18.600 回答
1

photoTaken已经被实例化为一个UIImage对象。无需这样做:

[photos addObject : [UIImage photoTaken]];

只需这样做:

[photos addObject : photoTaken];
于 2013-11-10T12:57:07.993 回答