ios 截取當前view並保存到相冊

//界面截取方法

+ (instancetype)captureWithView:(UIView *)view

{
    // 1.開啓上下文
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
    
    // 2.將控制器view的layer渲染到上下文
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    // 3.取出圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 4.結束上下文
    UIGraphicsEndImageContext();
    
    return newImage;

}


- (void)save {
    // 1.截圖
    UIImage *image = [UIImage captureWithView:self.view];
    
    // 2.保存到圖片
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}


/**
 保存圖片操作之後就會調用
 */
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) { // 保存失敗
        [MBProgressHUD showError:@"保存失敗"];
    } else { // 保存成功
        [MBProgressHUD showSuccess:@"保存成功"];
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章