我正在尝试使用 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/