에 의해 영상에 UIView의 변환 drawViewHierarchyInRect : afterScreenUpdates을 : 인 여러 번 빠르게 renderInContext 이상
중요 참고 사항 : viewDidLoad 또는 viewWillAppear 에서이 함수를 호출하지 마십시오 . 뷰가 완전히 표시 /로드 된 후 뷰를 캡처하고 있는지 확인하십시오.
Obj C
UIGraphicsBeginImageContextWithOptions(myView.bounds.size, myView.opaque, 0.0f);
[myView drawViewHierarchyInRect:myView.bounds afterScreenUpdates:NO];
UIImage *snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
myImageView.image = snapshotImageFromMyView;
편집 된 이미지 사진 앨범 저장
UIImageWriteToSavedPhotosAlbum(snapshotImageFromMyView, nil,nil, nil);
스위프트 3/4
UIGraphicsBeginImageContextWithOptions(myView.bounds.size, myView.isOpaque, 0.0)
myView.drawHierarchy(in: myView.bounds, afterScreenUpdates: false)
let snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
print(snapshotImageFromMyView)
myImageView.image = snapshotImageFromMyView
확장자, iOS11, swift3 / 4로 매우 쉽게 일반화
extension UIImage{
convenience init(view: UIView) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.init(cgImage: (image?.cgImage)!)
}
}
Use :
//myView is completly loaded/visible , calling this code after only after viewDidAppear is call
imgVV.image = UIImage.init(view: myView)
// Simple image object
let img = UIImage.init(view: myView)