본문 바로가기
iOS

화면 캡처 후 공유하기

by 냥이있는삶 2020. 5. 18.
반응형
//이미지 공유하기
- (void)captureView {

    //캡처
    UIGraphicsBeginImageContextWithOptions(self.contents.bounds.size, self.contents.opaque, 0.0);
    [self.contents.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //저장
    NSData *imageData = UIImagePNGRepresentation(img);
    NSString *writePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"sg.png"];
    if (![imageData writeToFile:writePath atomically:YES]) {
        // failure
        NSLog(@"image save failed to path %@", writePath);
        return;
    } else {
        // success.
    }

    NSURL *fileURL = [NSURL fileURLWithPath:writePath];

    //공유
    [self shareFile:fileURL];

}

- (void)shareFile:(NSURL *)localPath {
    _documentController = [[UIDocumentInteractionController alloc] init];
    _documentController.URL = localPath;
    _documentController.delegate = self;
    [_documentController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
}

- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller {
    _documentController = nil;
}
반응형