导航 View Controller 中的 IOS 图像选择器

标签 ios objective-c uiimagepickercontroller

我正在尝试使用 UIImagePickerController 浏览图像并在 imageview 中显示。基本上,我用来浏览图像的 View 是基于 navigationController 的。

但是在我选择图像并返回查看后使用下面的代码,我可以看到 View 在左右闪烁并且图像没有显示在 ImageView 中。

实际上整个源代码很长,这就是为什么我只发布图像选择器部分的原因。如果需要,我可以发布更多代码。

RegistrationFormViewController.h

@interface RegistrationFormViewController : ViewController
<UIImagePickerControllerDelegate,UINavigationControllerDelegate,CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    UIImagePickerController *imagePickerController;
}
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (weak, nonatomic) IBOutlet UIImageView *imageUser;

- (IBAction)registerBt:(id)sender;

RegistrationFormViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    _barButtonBack.target = self.revealViewController;
    _barButtonBack.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];


    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
    singleTap.numberOfTapsRequired = 1;
    [self.imageUser setUserInteractionEnabled:YES];
    [self.imageUser addGestureRecognizer:singleTap];
}



-(void)tapDetected{
    NSLog(@"single Tap on imageview");
    imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}

#pragma mark - ImagePickerController Delegate

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{
    // Dismiss the image selection, hide the picker and

    //show the image view with the picked image

    [picker dismissViewControllerAnimated:YES completion:nil];
    [self.imageUser setImage:image];

     self.imageUser.contentMode = UIViewContentModeScaleAspectFill;
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

最佳答案

您在此处显示的代码没有问题。但是在 didFinishPickingImage 中试试这个 -

dispatch_async(dispatch_get_main_queue(), ^{
    self.imageUser.contentMode = UIViewContentModeScaleAspectFill;
     [self.imageUser setImage:image];

    });

并将此代码移动到 viewDidLoad

imagePickerController = [[UIImagePickerController alloc]init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;

您还可以使用断点检查是否正在获取图像。

关于导航 View Controller 中的 IOS 图像选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37314997/

相关文章:

ios - Branch.IO showShareSheet 不生成 ShortUrl

ios - 如何在新搜索中取消 uitableviewcells 的异步图像加载

ios - Storyboard转场

iphone - 单击按钮后如何从 View Controller 导航到另一个 View Controller?

ios - Core Audio - 将数据写入音频文件的开头或中间(结尾以外的某个地方)

ios - 如何根据设备屏幕尺寸转换框架?

iphone - 横向的 UIImagePickerController

ios - 为什么视口(viewport)元常量设备宽度不为 ipad 3 返回 1536 像素而不是 768 像素?

ios7 - iOS 7 和 iOS 8 中 UIImagepickercontroller 的相机 View 上的状态栏重叠

ios - 如何在SwiftUI中打开ImagePicker?