2013-08-14 2 views
0

내 앱에서 사용자는 사진 위에 스티커를 붙일 수 있습니다. 그들은 그들의 창조를 저장 갈 때, 나는 화면 횡령을하고 UIImage에 저장 :화면 그랩에서 이미지 해상도 유지

UIGraphicsBeginImageContextWithOptions(self.mainView.bounds.size, NO, [UIScreen mainScreen].scale); 
[self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *resultImage = [UIGraphicsGetImageFromCurrentImageContext() retain]; 
UIGraphicsEndImageContext(); 

(self.mainView은 사진을 보유하고있는 하위 뷰 UIImageView, 다른 서브 뷰 스티커를 보유하고 UIView을 가지고 경우).

나는 이런 방식으로 스크린 샷을 찍을 수 있으며, 앞에서 설명한 사진의 해상도를 유지할 수 있습니까?

답변

1

다음 하나로 두 UIImage들 "평탄화"한다 :

CGSize photoSize = photoImage.size; 
UIGraphicsBeginImageContextWithOptions(photoSize, NO, 0.0); 

CGRect photoRect = CGRectMake(0, 0, photoSize.width, photoSize.height); 
// Add the original photo into the context 
[photoImage drawInRect:photoRect]; 
// Add the sticker image with its upper left corner set to where the user placed it 
[stickerImage drawAtPoint:stickerView.frame.origin]; 

// Get the resulting 'flattened' image 
UIImage *flattenedImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
(가) 상기 photoImage 가정

stickerImageUIImagestickerView의 두 경우는 stickerImage을 포함하는 UIView이므로 stickerView 프레임을 사용하여 해당 원점을 결정할 수 있습니다.

스티커가 여러 개인 경우 컬렉션을 반복하면됩니다.

0

현재보기의 이미지를 저장하려는 경우이 방법이 도움이 될 수 있습니다. 원 화상의 해상도 (들)을 유지하면서

UIGraphicsBeginImageContext(self.scrollView.contentSize); 

[self.scrollView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

CGImageRef imageRef = CGImageCreateWithImageInRect(finalImage.CGImage, 
                CGRectMake(scrollView.contentOffset.x,  scrollView.contentOffset.y, 
                   scrollView.frame.size.width, scrollView.frame.size.height)); 

UIImage *screenImage = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; 

CGImageRelease(imageRef);