2012-05-12 30 views
1

在我的應用程序中,用戶可以添加UIImageView作爲我的UIScrollView的子視圖。當用戶點擊「保存」按鈕時,我想將我的UIScrollView的所有內容保存爲UIImage到照片庫中。在UIImage中轉換UIScrollView的內容

我一直在谷歌上看,但沒有太多關於這個問題。

編輯:下面的代碼做這項工作

 CGRect rect = self.mainView.frame; 
     [self.mainView setFrame:CGRectMake(0, 0, 824, self.mainView.contentSize.height)]; 
     UIGraphicsBeginImageContext(self.mainView.bounds.size); 
     [self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     UIImageWriteToSavedPhotosAlbum(image , nil, nil, nil); 
     [self.mainView setFrame:rect]; 

回答

2

好像你需要採取屏幕的截屏,你可以嘗試上完成此代碼的運行 -

UIGraphicsBeginImageContext(self.view.bounds.size); 
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
NSData * data = UIImagePNGRepresentation(image); 
[data writeToFile:@"myImage.png" atomically:YES]; 
+0

不要忘記在渲染之前調整滾動視圖的大小以適應內容,然後將其重新調整爲原始大小。 –

+1

我得到這個錯誤與第二行: '接收器類型'CALayer'例如消息是一個前向聲明' – Benjamin

+1

添加此 - #import rishi