用户从库中选择UIImageView后拒绝`image` - iOS

时间:2014-09-17 22:27:58

标签: ios objective-c uiimageview

我有一些基本代码让用户选择一张图片并将其设置为已创建的UIImageView

- (void)addAvatarImageView {
    self.avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 83, 83)];
    self.avatarImageView.imageURL = [[NSURL alloc] initWithString:self.user[@"avatar"]];
    [self.avatarView addSubview:self.avatarImageView];
}

和选择器创建:

- (void)showAvatarPicker {
    UIImagePickerController *imagePickController = [[UIImagePickerController alloc] init];
    imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickController.delegate = self;
    imagePickController.allowsEditing = YES;
    [self.navigationController presentViewController:imagePickController animated:YES completion:nil];
}

和选择代表:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self.avatarImageView setImage:image];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

我可以在委托方法中使用断点并且它可以很好地进行,但UIImageView根本不显示新图像。我甚至无法删除存在的图像。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我将在这里继续讨论并说错误出现在您未显示的代码部分中:您实例化选择器的位置。更为明确的是,您可能没有将allowsEditing设置为是。所以看看下面的

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];

或者,您只需使用UIImagePickerControllerOriginalImage而不是当前的UIImagePickerControllerEditedImage阅读原始图片。

答案 1 :(得分:0)

更改UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];到UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];