iOS功能 - 使用UIRecognizer将图片添加手势(轻拍、捏合、拖动等)并存入系统相册
准备工作
1 - 和 iOS 10 保存图片、调用相机一样, 要 Info.plist 里面要涉及隐私数据时要添加一句提示语。打开 Info.plist 点击 + 号,在 Key 中输入:Privacy - Photo Library Additions Usage Description,Type 选 String,Value 中输入你的提示语。访问系统图库时用到的系统库时要引入头文 QuartzCore
2 - 代码示例:将图片添加手势(轻拍、捏合、拖动等),并实现截屏功能,最终截屏添加进系统相册。注:需要解决轻扫手势和拖动手势冲突的问题
#import "ViewController.h" #import <QuartzCore/QuartzCore.h> @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 存放图片 self.array = [NSArray arrayWithObjects: @"0F7129.jpg", @"5B6FBE.jpg", @"765F49.jpg", @"A45453.jpg", @"C6EECE.jpg", @"D0347B.jpg", nil]; int W_Main = [UIScreen mainScreen].bounds.size.width; float W_Pic = (W_Main - 20*4)/3.0;// 图片宽度 float H_Pic = W_Pic*1.3; // 图片高度 int n = 0;// 记录数组下标 // 2行3列 for (int i = 0; i < 2; i ++) { for (int j = 0; j < 3; j ++) { self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(W_Pic*j +20*(j+1), 80+H_Pic*i+20*(i+1), W_Pic, H_Pic)]; self.imageView.userInteractionEnabled = YES;// 打开交互 self.imageView.image = [UIImage imageNamed:[self.array objectAtIndex:n]]; [self.view addSubview:self.imageView]; [self.imageView release]; n++; // 旋转手势j UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)]; [self.imageView addGestureRecognizer:rotationGesture]; // 拖动手势 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; [self.imageView addGestureRecognizer:panGesture]; // 捏合:实现缩放 UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)]; [self.imageView addGestureRecognizer:pinchGesture]; // 双击:先缩小、后放大 UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGesture:)]; doubleTapGesture.numberOfTapsRequired = 2;// 设置双击; [self.imageView addGestureRecognizer:doubleTapGesture]; // 长按:删除 UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; longPressGesture.minimumPressDuration = 2.0;// 长按时间 [self.imageView addGestureRecognizer:longPressGesture]; // 轻扫手势 UISwipeGestureRecognizer * swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)]; swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; [self.imageView addGestureRecognizer:swipeGesture]; swipeGesture.direction =UISwipeGestureRecognizerDirectionLeft |UISwipeGestureRecognizerDirectionRight; // 解决拖动和轻扫的冲突 [panGesture requireGestureRecognizerToFail:swipeGesture]; } } // 截图 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(110, 500, W_Main-220, 30); button.backgroundColor = [UIColor cyanColor]; [button setTitle:@"保存" forState:UIControlStateNormal]; [button setTitle:@"保存中..." forState:UIControlStateHighlighted]; [button addTarget:self action:@selector(saveIt) forControlEvents: UIControlEventTouchUpInside]; [self.view addSubview:button]; button.tag = 111; } // 轻扫 - (void)handleSwipeGesture:(UISwipeGestureRecognizer *)swipeGesture{ //声明变量,随机图片下标 int m = arc4random()%6; UIImageView *swipeImageView = (UIImageView *)swipeGesture.view; swipeImageView.image= [UIImage imageNamed:[self.array objectAtIndex:m]]; } // 双击缩放 - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)tapGesture{ // 如果图片宽度为 120 if (tapGesture.view.bounds.size.width==120) { tapGesture.view.bounds = CGRectMake(0, 0, tapGesture.view.frame.size.width/3, tapGesture.view.frame.size.height/3); // 放大1.5倍 }else{ tapGesture.view.bounds = CGRectMake(0, 0, tapGesture.view.bounds.size.width*1.5, tapGesture.view.bounds.size.height*1.5); } } // 长安删除 - (void)handleLongPressGesture:(UILongPressGestureRecognizer *)longPressGesture{ if (longPressGesture.state ==UIGestureRecognizerStateBegan) { [longPressGesture.view removeFromSuperview]; } } // 捏合 - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinchGesture{ pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale); pinchGesture.scale = 1.0;// 注意初始化 } // 拖动 - (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture{ CGPoint offSet = [panGesture translationInView:panGesture.view]; panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, offSet.x, offSet.y); [panGesture setTranslation:CGPointZero inView:panGesture.view]; } // 旋转 - (void)handleRotationGesture:(UIRotationGestureRecognizer *)rotationGesture{ rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation); rotationGesture.rotation = 0.0; } // 存储:保存到相册 - (void)saveIt{ // 截屏大小 UIGraphicsBeginImageContext(self.view.bounds.size); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // 写入相册 UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil); // 提示框 UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"存储成功" message:@"您的图片成功保存到相册" delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil]; [alert show]; } @end
运行效果:截屏
素材地址
https://pan.baidu.com/s/15k169qXviuWvjZQCPPIDNQ
1jau
分类:
iOS功能封装
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)