iOS 应用程序截图和保存至系统相册

上源码

- (void)saveImage

{

    dispatch_async(dispatch_get_main_queue(), ^{

        //截取图片

        UIImage *cropImaged = [self imageFromView:self atFrame:_pBottomView.frame];

        //保存到系统相册

        UIImageWriteToSavedPhotosAlbum(cropImaged, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    });

    

    

}


- (UIImage *)imageFromView:(UIView *)theView atFrame:(CGRect)r

{

    UIGraphicsBeginImageContext(theView.frame.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    UIRectClip(r);

    [theView.layer renderInContext:context];

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return  theImage;

}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo: (void *) contextInfo;

{

    NSString *pMsg = @"保存成功";

    if (error) {

        pMsg = error.domain;

    }

    

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:MYCapitalCycleAlert message:pMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

    [alertView show];

}


分析下保存图片的函数:

UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo)


第一个参数、第二个参数略;

第三个参数如果不查文档不会知道怎么写。所以官方文档就是这样的一个响应函数。

第四个参数还不知道怎么用。我传了nil。


其实很简单。



你可能感兴趣的:(系统相册,保存图片,程序截图)