最近的项目中运用到了这两个功能,所以记录一下。做了一个小工程将两个方法结合到了一起
1
- (void)viewDidLoad { [super viewDidLoad]; UIButton * btn = [UIButton buttonWithType:UIButtonTypeContactAdd]; btn.frame =CGRectMake(30, 30, 30, 30); [self.view addSubview:btn]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; } -(void)btnClick:(UIButton *)sender { [self savePhoto]; } // 保存图片到相册功能,ALAssetsLibraryiOS9.0 以后用photoliabary 替代, -(void)savePhoto { UIImage * image = [self captureImageFromView:self.view]; ALAssetsLibrary * library = [ALAssetsLibrary new]; NSData * data = UIImageJPEGRepresentation(image, 1.0); [library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:nil]; } //截图功能 -(UIImage *)captureImageFromView:(UIView *)view { CGRect screenRect = [view bounds]; UIGraphicsBeginImageContext(screenRect.size); CGContextRef ctx = UIGraphicsGetCurrentContext(); [view.layer renderInContext:ctx]; UIImage * image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
时间: 2024-12-21 12:41:00