2016-03-02 75 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; 

} 
相關問題