将UIView中的图像保存到相册

 1 - (IBAction)btnClicked:(id)sender {
 2     UIImage *image = [self getImageFormView:self.editView];
 3     
 4     UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
 5     
 6 }
 7 //UIImageWriteToSavedPhotosAlbum方法所需要的回调方法
 8 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
 9     if (error) {
10         NSLog(@"%@",[error localizedDescription]);
11     }else{
12         UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存成功!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
13         [av show];
14         [av release];
15     }
16 }
17 
18 -(UIImage*)getImageFormView:(UIView*)view{
19     //创建一个画布
20     UIGraphicsBeginImageContext(view.frame.size);
21     //将view的内容渲染到画布中
22     [view.layer renderInContext:UIGraphicsGetCurrentContext()];
23     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
24     //结束
25     UIGraphicsEndImageContext();
26     return image;
27 }

 

posted @ 2013-07-11 22:24  ubersexual  阅读(241)  评论(0编辑  收藏  举报