2013-08-06 31 views
1

我有uiview,我想將該視圖轉換爲工作得很好的圖像,但是當我按下iphone的主頁按鈕並再次運行應用程序時,圖像變得放大。這裏是我將代碼轉換爲圖像的代碼。爲什麼在將視圖轉換爲圖像時圖像變焦?

When the app 1st time run

After pressing the home button and run the app again.

- (UIImage *)captureView { 
     CGRect rect = [myview bounds]; 
     UIGraphicsBeginImageContext(rect.size); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 
     [myview.layer renderInContext:context]; 
     UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return img; 

} 
checkImage.image = [self captureView]; 

回答

1

這裏這段代碼沒有錯,但是你使用了一些其他的對象而不是整個視圖。我不知道,但嘗試使用視圖邊界在整個象顯示..見我的回答同樣喜歡你從這個波紋管鏈接這個答案..

how to capture uiview top uiview

還與self.view嘗試,而不是myView在你的代碼中。

添加此代碼,而不是你checkImage.image = [self captureView];線..

UIImageView *imgTempHeader = [[UIImageView alloc]initWithImage:[self captureView]]; 
imgTempHeader.frame = self.view.frame; 
[self.view addSubview:imgTempHeader]; 
[self.view bringSubviewToFront:imgTempHeader]; 
+0

myView是主視圖的另一種視圖 –

+0

是的,我知道但我只是想檢查,如果你在所有的代碼中使用self.view而不是myView,那麼它會得到整個視圖的圖像或不是..做到這一點,讓我知道,也使用我的代碼,而不是你從這個鏈接http:///stackoverflow.com/questions/14049796/howe-to-capture-uiview-top-uiview/ –

+0

@parash喬希同樣的問題 –

0

你應該創造一種方式即使用設備的規模方面:

UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); 

然後,當您在圖像中顯示的圖像視圖確保將內容模式設置爲防止縮放。

+0

嘗試過但不工作 –

+0

'myview'和'checkImage'的大小是多少? – Wain

+0

都有相同的大小 –

0

我的經驗是,在IB建立imageViews。我認爲checkImage是UIImageView的一個實例?

如果您在UIImageView中顯示圖像,並且只將視圖模式設置爲在界面構建器中居中,則在圖像很大時會顯示放大圖像。

看一看這種方式先處理你的形象:

UIImage *newImage = [[UIImage alloc] initWithCGImage:myCGImageRef scale:myZoomSCale orientation:UIImageOrientationDown]; 

當然,你需要給它一個CGImageRef爲你的形象。對你或其他人來說。

+0

不工作先生給我一些確切的答案 –