2016-03-02 118 views
1

我在我的应用双视图控制器,一个控制器捕捉使用AVCapturSession的iOS的UIImageView不显示图像

我写这一张照片,

[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其图像I我设置为显示图像,但图像未设置。

我SecondViewController从故事板制作,

的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中为什么?

回答

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]; 
} 
+0

@安布。Karthik先生,showTakenPic是IBOutlet,他必须从故事板强制加载 – techloverr

+0

我同意你的观点,也认为这种情况下,'imageData'转换页面之前导航也不会出现图像 –

+0

我的SecondViewController不是一个导航控制器,所以我可以像这样推动它? –

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"); 
} 
+0

显示图像不是零,但即使设置图像UIImageView显示无 –

+0

你试过我的代码? –

+0

是的,仍然给我一个黑屏 –

0

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

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]; 

您可以添加此代码到viewDidLoad中或SecondViewController

self.showTakenPic.image = self.imageToSet 
的viewWillAppear中

,或者你正在使用SEGUE什么!

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; 

}