2012-12-21 180 views
1

我試圖獲取另一個控制器視圖的快照以在動畫中使用。我使用下面的代碼,但我什麼也看不見,但這種觀點的背景顏色,並沒有其子視圖(兩個按鈕,一個文本視圖和標籤):視圖快照不渲染

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    FirstPageController *fpController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstPage"]; 
    UIGraphicsBeginImageContext(fpController.view.bounds.size); 
    NSLog(@"%@",fpController.view.subviews); 
    [fpController.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    CGSize destinationSize = CGSizeMake(90, 122); 

    UIGraphicsBeginImageContext(destinationSize); 
    [viewImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    self.imageView2 = [[UIImageView alloc] initWithImage:newImage]; 
    self.imageView2.center = self.view.center; 
    [self.view addSubview:self.imageView2]; 
    NSLog(@"%@",NSStringFromCGSize(self.imageView2.image.size)); 
} 

我記錄的所有相關的東西,fpController,imageView,圖像和所有正確記錄。

編輯完成後:如果我切換到當前控制器的視圖,它工作正常,所以我認爲這與獲取不在屏幕上的視圖的快照有關。可以這樣做嗎?

+0

您是否嘗試過在上下文中渲染各個子視圖? –

+0

不,從對類似問題的答案,因爲我的理解是,這將使所有子視圖 – rdelmar

回答

1
UIGraphicsBeginImageContextWithOptions(fpController.view.bounds.size, NO, 0.0); 


UIGraphicsBeginImageContextWithOptions(destinationSize, NO, 0.0); 

使用此兩行而不是UIGraphicsBeginImageContext(fpController.view.bounds.size);

UIGraphicsBeginImageContext(destinationSize); respectively 

希望它能幫助是你

+0

我得到相同的結果 - 只是視圖的背景顏色。 – rdelmar

+0

self.imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageWithData:UIImagePNGRepresentation(newImage)]];添加這個,並嘗試 – MadhuP

3

希望這將幫助你:

CGSize textcontent =CGSizeMake(width, height); 
UIGraphicsBeginImageContext(textcontent); 
if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f) 
{ 
    UIGraphicsBeginImageContextWithOptions(textcontent, YES, 1.0f); 
} 
else 
{ 
    UIGraphicsBeginImageContext(textcontent); 
} 
    [image.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return viewImage; 
+0

不,這不會幫助 - 這個問題似乎與我試圖呈現的視圖不在屏幕的事實有關。 – rdelmar

0

我找到了一個方法,使這項工作,儘管它看起來像一個黑客。我將我想要渲染的視圖添加爲視圖的子視圖,進行渲染,然後刪除視圖。它從來沒有在屏幕上看到,所以視覺上,它很好。我只是想知道是否有更好的方法。

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    self.fpController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstPage"]; 

    [self.view insertSubview:self.fpController.view belowSubview:self.view]; 

    UIGraphicsBeginImageContext(self.fpController.view.bounds.size); 
    [self.fpController.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    [self.fpController.view removeFromSuperview]; 

    self.imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 122)]; 
    self.imageView2.image = viewImage; 
    [self.pageView insertSubview:self.imageView2 belowSubview:self.imageView]; 
} 
+0

你可以寫一個與fpController函數相同的代碼來返回一個Image嗎?如 - (UIImage *)returnRasterized;那麼你可以調用[self.imageView2 setImage:[self.fpController returnRasterized]];也許少一點'駭客' – Gallonallen