iOS : 프로그래밍 방식으로 스크린 샷을 만드는 가장 빠르고 효과적인 방법은 무엇입니까?
내 iPad 앱에서 화면의 큰 부분을 차지하는 UIView의 스크린 샷을 만들고 싶습니다. 안타깝게도 하위 뷰는 매우 깊숙이 중첩되어 스크린 샷을 만들고 나중에 페이지 컬링을 애니메이션하는 데 시간이 오래되어야합니다.
"일반적인"방법보다 빠른 방법이 있습니까?
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
가능하다면 내 뷰를 캐싱하거나 내장하는 것을 피하고 싶습니다.
가능할 때마다 스냅 샷 API를 사용하는 더 나은 방법을 찾았습니다.
도움이되기를 바랍니다.
class func screenshot() -> UIImage {
var imageSize = CGSize.zero
let orientation = UIApplication.shared.statusBarOrientation
if UIInterfaceOrientationIsPortrait(orientation) {
imageSize = UIScreen.main.bounds.size
} else {
imageSize = CGSize(width: UIScreen.main.bounds.size.height, height: UIScreen.main.bounds.size.width)
}
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
for window in UIApplication.shared.windows {
window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
Objective-C 버전 :
+ (UIImage *)screenshot
{
CGSize imageSize = CGSizeZero;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation)) {
imageSize = [UIScreen mainScreen].bounds.size;
} else {
imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
}
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, window.center.x, window.center.y);
CGContextConcatCTM(context, window.transform);
CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
if (orientation == UIInterfaceOrientationLandscapeLeft) {
CGContextRotateCTM(context, M_PI_2);
CGContextTranslateCTM(context, 0, -imageSize.width);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, 0);
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
}
if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
} else {
[window.layer renderInContext:context];
}
CGContextRestoreGState(context);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
2013 년 10 월 3 일 수정 iOS 7에서 새로운 초고속 drawViewHierarchyInRect : afterScreenUpdates : 메소드를 지원하도록 업데이트되었습니다.
아니요. CALayer의 renderInContext :이 작업을 수행하는 유일한 방법을 알고 있습니다. 다음과 같이 UIView 카테고리를 생성하여 더 쉽게 진행할 수 있습니다.
UIView + Screenshot.h
#import <UIKit/UIKit.h>
@interface UIView (Screenshot)
- (UIImage*)imageRepresentation;
@end
UIView + Screenshot.m
#import <QuartzCore/QuartzCore.h>
#import "UIView+Screenshot.h"
@implementation UIView (Screenshot)
- (UIImage*)imageRepresentation {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, self.window.screen.scale);
/* iOS 7 */
if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
else /* iOS 6 */
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* ret = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return ret;
}
@end
통해 뷰 컨트롤러 [self.view.window imageRepresentation]
에서 말할 수 있고이를 앱의 전체 스크린 샷을 얻을 수 있습니다. 이 상태 표시 줄을 제외 할 수 있습니다.
편집하다 :
그리고 추가 할 수 있습니다. 투명한 콘텐츠가있는 UIView가 있고 언더 레이 콘텐츠가 포함 된 이미지 표현이 필요한 경우, 포함 하위 뷰의 사각형을 가져와 컨테이너로 변환하여 컨테이너 뷰의 이미지 표현을 가져와 해당 이미지를 사용할 수 있습니다. 좌표계를 봅니다.
[view convertRect:self.bounds toView:containerView]
자르려면이 질문에 대한 답변을 참조하십시오 . UIImage 자르기
iOS 7에는 현재 그래픽 컨텍스트에 뷰 계층 구조를 그릴 수있는 새로운 방법이 도입되었습니다. 이것은 UIImage
매우 빠르게 하는 데 사용할 수 있습니다 .
에 카테고리 방법으로 구현 UIView
:
- (UIImage *)pb_takeSnapshot {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
기존 renderInContext:
방법 보다 훨씬 빠릅니다 .
UPDATE FOR SWIFT : 동일한 기능을 수행하는 확장 :
extension UIView {
func pb_takeSnapshot() -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.mainScreen().scale);
self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
// old style: self.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
레티 나 또는 비 유지 장치에서도 모든 iOS 버전에서 실행될 단일 기능에 대한 답변을 결합했습니다.
- (UIImage *)screenShot {
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.view.bounds.size);
#ifdef __IPHONE_7_0
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
#endif
#else
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
#endif
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
나를 위해 InterpolationQuality 설정은 먼 길을 갔다.
CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
매우 상세한 이미지를 스냅 샷하는 경우이 솔루션이 허용되지 않을 수 있습니다. 텍스트 스냅 샷을 찍는 경우 차이를 거의 알아 차리지 못할 것입니다.
이렇게하면 스냅 샷을 찍는 시간이 크게 단축되고 메모리를 훨씬 적게 소비하는 이미지를 만들 수 있습니다.
이것은 drawViewHierarchyInRect : afterScreenUpdates : 메소드를 사용하면 여전히 유용합니다.
대안으로 요구하는 것은 GPU를 읽는 것입니다 (화면이 여러 반투명 뷰에서 합성되기 때문에). 이는 본질적으로 느린 작업이기도합니다.
'ProgramingTip' 카테고리의 다른 글
NUnit.Framework.Assert.IsInstanceOfType ()은 더 이상 사용되지 않습니다. (0) | 2020.10.14 |
---|---|
Android에서 PDF를 방법하는 방법 (0) | 2020.10.14 |
std :: set을 반복하는 방법? (0) | 2020.10.14 |
Android의 Google 클라우드 메시징에서 등록 ID 변경 처리 (0) | 2020.10.14 |
Node.js + Express 웹 애플리케이션을 패키징하고 배포하는 방법은 무엇입니까? (0) | 2020.10.14 |