无法显示从照片库中挑选的图像

Posted

技术标签:

【中文标题】无法显示从照片库中挑选的图像【英文标题】:can't show picking image from photo library 【发布时间】:2013-04-04 05:00:58 【问题描述】:

我正在使用图像选择器从照片库中选择照片。当我单击按钮时,它会显示照片库。但是当我单击特定图像时,它会关闭 modalView 并且图像不会加载到查看。

代码:

-(void)showcamera:(id)sender

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

    [self presentModalViewController:imagePickerController animated:YES];




- (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 dismissModalViewControllerAnimated:YES];
    UIImage *newImage = image;



【问题讨论】:

我认为它的 bcoz 您在关闭选择器后设置 newImage。这是我的意见。您还必须初始化图像。 为什么还要初始化两次imagePickerController? 当我使用 imagePickerController.allowsEditing=YES;编码工作完美。谢谢大家 当我使用 imagePickerController.allowsEditing=YES;矩形框和导航栏内显示的图像,带有选择、取消按钮。我不要盒子。如何取消? 【参考方案1】:

使用此代码:

在.h文件中


UIImageView *newImage;
    UIImagePickerController *imagePicker;

在 .m 文件中

- (void)imageTaped:(UITapGestureRecognizer *)tapGesture 
    imagePicker=[[UIImagePickerController alloc]init];

    imagePicker.delegate = self;

    imagePicker.allowsEditing =NO;

    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentModalViewController:imagePicker animated:YES];





-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    //release picker
    [picker dismissModalViewControllerAnimated:YES];

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    //set image


    newImage = [[UIImageView alloc] initWithImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
    [self.view addSubview:mewImage];
    [picker dismissModalViewControllerAnimated:YES];

【讨论】:

当我使用 imagePickerController.allowsEditing=YES;矩形框和导航栏内显示的图像,带有选择,取消按钮。我不要盒子。如何取消? 抱歉,我的代码中我使用的是图像裁剪功能,这就是原因。只需执行imagePicker.allowsEditing =NO;,它就不会显示该框。 你必须把这行[info objectForKey:UIImagePickerControllerEditedImage]改成[info objectForKey:UIImagePickerControllerOriginalImage] 但是如何从图像中返回..在视图中加载图像后我可以添加这一行 [picker dismissModalViewControllerAnimated:YES];内部按钮按下取消 我们在取消按钮中添加了[picker dismissModalViewControllerAnimated:YES];。我们不是吗?还是你的意思是别的?【参考方案2】:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

    imgPicked = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    
        [imagePickerController dismissModalViewControllerAnimated:YES];
    

在 .h 中声明

UIImage *imgPicked; 

ViewDidLoad

imgPicked = [[UIImage alloc]init];

【讨论】:

什么?我找不到你...拿一个 图片从库中选取后不显示 当我使用 imagePickerController.allowsEditing=YES;时,您的代码也可以工作。【参考方案3】:

最好的方法是将您的Image 添加到UIImageView

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

     UIImageView *ImgView = [[UIImageView alloc] initWithFrame:CGRectMake(@"As U Want")];
     ImgView.image = image;
    [self.View addSubview: ImgView];


    [picker dismissModalViewControllerAnimated:YES];

【讨论】:

以上是关于无法显示从照片库中挑选的图像的主要内容,如果未能解决你的问题,请参考以下文章

刚从相机拍摄并通过从图库中挑选显示的Android裁剪图像

如何在 CollectionView 中显示从照片库或相机中选择的图像?画廊视图

快速检测从照片库中删除的图像

仅在 Pixel_Api_26 模拟器中从相机拍摄照片后,无法在 ImageView 中放置图像

无法使用 h:graphicImage JSF 2.0 显示图像

在 imageview 中显示图像时代码崩溃