iOS截图和保存图片
2016-03-30 11:57
344 查看
最近整理一下以往的项目,分享一段截图和保存图片的代码,刚开始的时候写这东西的时候,有些凌乱,现在看来还是比较简单的,算是记录一下。直接上代码了,有注释,可以直接套用。
// // ViewController.m // Test // // Created by york on 16/3/30. // Copyright © 2016年 裴杨威. All rights reserved. // #define DEVICE_WIDTH [UIScreen mainScreen].bounds.size.width #define DEVICE_HEIGHT [UIScreen mainScreen].bounds.size.height #import "ViewController.h" @interface ViewController () { UIImageView *topImageView; UIImageView *bottomImageView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor grayColor]; topImageView = [[UIImageView alloc] initWithFrame:CGRectMake((DEVICE_WIDTH-252)/2, 20, 252, 220)]; topImageView.image = [UIImage imageNamed:@"1.jpg"]; [self.view addSubview:topImageView]; bottomImageView = [[UIImageView alloc] initWithFrame:CGRectMake((DEVICE_WIDTH-140)/2, CGRectGetMaxY(topImageView.frame), 140, 220)]; bottomImageView.image = [UIImage imageNamed:@"2.jpg"]; [self.view addSubview:bottomImageView]; UIButton *saveTopImageBtn = [UIButton buttonWithType:UIButtonTypeSystem]; saveTopImageBtn.frame = CGRectMake(10, CGRectGetMaxY(bottomImageView.frame)+20, (DEVICE_WIDTH-30)/2, 30); [saveTopImageBtn setTitle:@"截图上图" forState:0]; saveTopImageBtn.tag = 100; [saveTopImageBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:saveTopImageBtn]; UIButton *saveBottomImageBtn = [UIButton buttonWithType:UIButtonTypeSystem]; saveBottomImageBtn.frame = CGRectMake(CGRectGetMaxX(saveTopImageBtn.frame)+10, CGRectGetMaxY(bottomImageView.frame)+20, (DEVICE_WIDTH-30)/2, 30); [saveBottomImageBtn setTitle:@"截图下图" forState:0]; saveBottomImageBtn.tag = 101; [saveBottomImageBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:saveBottomImageBtn]; UIButton *saveAllImageBtn = [UIButton buttonWithType:UIButtonTypeSystem]; saveAllImageBtn.frame = CGRectMake(10, CGRectGetMaxY(saveTopImageBtn.frame)+20, (DEVICE_WIDTH-30)/2, 30); [saveAllImageBtn setTitle:@"截图整屏" forState:0]; saveAllImageBtn.tag = 102; [saveAllImageBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:saveAllImageBtn]; UIButton *SaveNetImageBtn = [UIButton buttonWithType:UIButtonTypeSystem]; SaveNetImageBtn.frame = CGRectMake(CGRectGetMaxX(saveTopImageBtn.frame)+10, CGRectGetMaxY(saveBottomImageBtn.frame)+20, (DEVICE_WIDTH-30)/2, 30); [SaveNetImageBtn setTitle:@"保存网络图片" forState:0]; SaveNetImageBtn.tag = 103; [SaveNetImageBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:SaveNetImageBtn]; } - (BOOL)isOpenAlbum//判断是否能打开相册 { if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) { return YES; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"无法打开相册" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; return NO; } } #pragma mark - #pragma mark --保存通过url获取的图片-- - (void)saveNetImage { NSData *dateImg = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://d.hiphotos.baidu.com/image/h%3D200/sign=8663264274f082023292963f7bfbfb8a/f3d3572c11dfa9eca13b947665d0f703918fc1be.jpg"]]; UIImage *image = [UIImage imageWithData:dateImg]; UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil); } #pragma mark - #pragma mark --截图-- - (UIImage *)getRealImage:(UIView *)view { /* UIGraphicsBeginImageContextWithOptions(<#CGSize size#>, <#BOOL opaque#>, <#CGFloat scale#>) size ----> 新创建的位图上下文的大小 opaque ----> 透明度开关 scale ----> 缩放因子 0指的是系统会自动设置正确的比例 */ UIGraphicsBeginImageContextWithOptions(view.bounds.size,YES,0);//创建一个基于位图的上下文 [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } #pragma mark - #pragma mark --系统保存图片的方法-- - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { if (!error) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"保存成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; }else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"保存失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; } } #pragma mark - #pragma mark --按钮点击事件-- - (void)saveBtnClick:(UIButton *)sender { switch (sender.tag) { case 100: { if ([self isOpenAlbum]) { UIImageWriteToSavedPhotosAlbum([self getRealImage:topImageView], self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil); } } break; case 101: { if ([self isOpenAlbum]) { UIImageWriteToSavedPhotosAlbum([self getRealImage:bottomImageView], self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil); } } break; case 102: { if ([self isOpenAlbum]) { UIImageWriteToSavedPhotosAlbum([self getRealImage:self.view], self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil); } } break; case 103: { if ([self isOpenAlbum]) { [self saveNetImage]; } } break; default: break; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- iOS quartzCore第二章——CAEAGLLayer简介
- iOS应用运用设计模式中的Strategy策略模式的开发实例
- iOS 关于scrollView内部子控件的自动布局
- iOS开发常用的资源和资料
- iOS 打电话 带提示框
- iOSCoreAnimation动画系列教程(一):CABasicAnimation
- IOS开发--使用lookup table为图片添加滤镜
- ios调用百度、高德地图 导航功能
- iOS图片如何按比例显示
- iOS沙盒目录解析
- iOS证书分类
- iOS App设计模式开发中策略模式的实现示例
- IOS 代理模式 DELEGATE
- iOS自带的Social.framework 自带的社交分享SDK
- IOS编译错误
- iOS 百度地图使用详解
- iOS中nRF蓝牙设备的OTA升级(一些设置)
- 举例讲解iOS应用开发中对设计模式中的策略模式的使用
- iOS之简单瀑布流的实现
- ios 线程死锁