iOS 가 view 를 캡 처 하 는 예제 코드
WKWebView 를 캡 처 해 야 합 니 다.그 전에 아래 의 방법 을 사 용 했 습 니 다.높 은 버 전의 시스템 은 문제 가 없 지만 낮은 버 전의 것 은 흰 그림 으로 캡 처 했 습 니 다.
- (UIImage *)convertViewToImage:(UIView *)view{
// 。 , NO, YES。
UIGraphicsBeginImageContextWithOptions(CGSizeMake(view.bounds.size.width, view.bounds.size.height * 0.8),YES,[UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
살 펴 본 결과 등급 에 문제 가 있 는 것 을 발 견 했 습 니 다.맨 위 에 있 는 보 기 를 자 르 지 못 했 습 니 다.그래서 아래 의 방법 으로 바 꾸 면 됩 니 다.
- (UIImage*)captureView:(UIView *)theView frame:(CGRect)frame{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(theView.bounds.size.width, theView.bounds.size.height*0.8), YES, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage *img;
if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0){
for(UIView *subview in theView.subviews){
[subview drawViewHierarchyInRect:subview.bounds afterScreenUpdates:YES];
}
img = UIGraphicsGetImageFromCurrentImageContext();
}else{
CGContextSaveGState(context);
[theView.layer renderInContext:context];
img = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
return img;
}
frame 이 비어 있 으 면 안 되 고 너무 빨리 자 르 면 문제 가 생 길 수 있 으 므 로 after ScreenUpdates 를 no 로 설정 해 야 합 니 다.YES 로 설정 하면 view update 가 끝나 고 실행 되 기 를 기다 리 기 때 문 입 니 다.update 가 끝나 기 전에 view 가 release 되면 view 를 찾 을 수 없 는 문제 가 발생 할 수 있 습 니 다.또한 UIGraphics BeginImageContextWith Options 를 사용 하여 고 화질 그림 을 캡 처 하 는 것 을 기억 하 세 요.이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
View의 레이아웃 방법을 AutoLayout에서 따뜻한 손 계산으로 하면 성능이 9.26배로 된 이야기이 기사는 의 15 일째 기사입니다. 어제는 에서 이었습니다. 손 계산을 권하는 의도는 없고, 특수한 상황하에서 계측한 내용입니다 화면 높이의 10 배 정도의 contentView가있는 UIScrollView 레이아...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.