iOS 가 view 를 캡 처 하 는 예제 코드

2017 단어 iOSview캡 처
본 고 는 주로 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 를 사용 하여 고 화질 그림 을 캡 처 하 는 것 을 기억 하 세 요.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기