iOS UIImageView无法显示图像

时间:2016-03-02 12:22:39

标签: ios objective-c uinavigationcontroller uiimageview

我的应用程序中有两个视图控制器,一个控制器使用AVCapturSession捕获照片

我写这个,

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
     if (exifAttachments)
     {
         // Do something with the attachments.
         NSLog(@"attachements: %@", exifAttachments);
     }
     else
         NSLog(@"no attachments");

     NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *displayimage = [[UIImage alloc] initWithData:imageData];

     SecondViewController *sec = [[SecondViewController alloc]init];
     sec.showTakenPic.image = displayimage;
     [self.navigationController pushViewController:sec animated:YES];
 }];

SecondViewController包含一个UIImageView,我的图像设置为显示图像,但图像没有设置。

我的SecondViewController是由storyboard制作的,

SecondViewController.h看起来像这样,

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property(atomic,retain) IBOutlet UIImageView *showTakenPic;

@end

SecondViewController.m看起来像这样,

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

-(void)viewDidLoad {
 [super viewDidLoad];
 [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

图像在UIImageView中看不到为什么?

5 个答案:

答案 0 :(得分:0)

试试这个

您必须先从故事板

加载控制器

Instantiating a View Controller programmatically with Storyboard from AppDelegate

根据此图片enter image description here

设置标识符
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
 SecondViewController *controller = (SecondViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: @"SecondViewController"];

然后

dispatch_async(dispatch_get_main_queue(), ^{
    sec.showTakenPic.image = displayimage;
});

这是主线程问题

<强>选择-2

if (imageData)
{
 UIImage *displayimage = [[UIImage alloc] initWithData:imageData];

 SecondViewController *sec = [[SecondViewController alloc]init];
 sec.showTakenPic.image = displayimage;
 [self.navigationController pushViewController:sec animated:YES];
}

答案 1 :(得分:0)

首先,检查图像是否为零。 其次,按下视图控制器后尝试设置图像。

if (displayimage)
{
     SecondViewController *sec = [[SecondViewController alloc]init];
     [self.navigationController pushViewController:sec animated:YES];
     sec.showTakenPic.image = displayimage;
     sec.showTakenPic.backgroundColor = [UIColor redColor];
}
else
{
    NSLog(@"displayimage is nill");
}

答案 2 :(得分:0)

图像有多大,颜色是什么?如果图像比UIImageView大,并且没有正确设置ContentMode属性,则可能只会看到图像的黑色部分。

答案 3 :(得分:0)

尝试在内存中加载SecondViewController时设置图像..使用UIImage类型的ivar,当推入堆栈的控制器使用ivar在showTakenPic imageView上设置图像时。

UIStoryboard mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
 SecondViewController *controller = (SecondViewController *)  
[mainStoryboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
controller.imageToSet = imagePickedFromFirstController
[self.navigationController pushViewController:controller];

你可以将这段代码添加到SecondViewController的viewDidLoad或viewWillAppear中

self.showTakenPic.image = self.imageToSet

或你用于segue的任何东西!

答案 4 :(得分:0)

您应该将图像间接设置到第二个视图控制器中。

FirstViewController.m

SecondViewController *postImageViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

[postImageViewController setImage:image];

SecondViewController.h

@interface SecondViewController : UIViewController {

    UIImage * postImage;
}

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

- (void)setImage:(UIImage*)image;

@end

SecondViewController.m

- (void)viewWillAppear:(BOOL)animated {

    if(postImage) {

        _imageView.image = postImage;

    }

}

- (void)setImage:(UIImage*)image {

    postImage = image;

}