模拟系统照相机图片裁剪的功能

简介:

模拟系统照相机图片裁剪的功能

效果如下:

源码:

//
//  RootViewController.m
//  ScrollView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UIScrollViewDelegate>

{
    BOOL tapped;
}

@property (nonatomic, strong) UIScrollView           *scrollView;
@property (nonatomic, strong) UIImageView            *imageView;
@property (strong, nonatomic) UITapGestureRecognizer *tapGesture;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    
    // scrollView
    {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        [self.view addSubview:_scrollView];
        
        _scrollView.center              = self.view.center;
        _scrollView.delegate            = self;
        _scrollView.layer.borderWidth   = 2.f;
        _scrollView.layer.borderColor   = [UIColor redColor].CGColor;
        _scrollView.layer.masksToBounds = NO;
        
        // 不显示滚动的条
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator   = NO;

        _scrollView.bouncesZoom      = YES;
        _scrollView.minimumZoomScale = 1.f;
        _scrollView.maximumZoomScale = 10.f;
        _scrollView.contentMode      = UIViewContentModeScaleAspectFit;
    }
    
    // 图片
    _imageView       = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    _imageView.image = [UIImage imageNamed:@"back"];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_scrollView addSubview:_imageView];
    
    // 手势
    _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                          action:@selector(tapRecognized:)];
    [_scrollView addGestureRecognizer:_tapGesture];
}

- (void)tapRecognized:(id)sender
{
    if (!tapped)
    {
        CGPoint tapPoint = [self.tapGesture locationOfTouch:0
                                                     inView:self.tapGesture.view];
        CGRect zoomRect = [self zoomRectForScrollView:self.scrollView
                                            withScale:6.0f
                                           withCenter:tapPoint];
        [self.scrollView zoomToRect:zoomRect animated:YES];
        tapped = YES;
    }
    else
    {
        [self.scrollView setZoomScale:1.0f animated:YES];
        tapped = NO;
    }
}


- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView
                      withScale:(float)scale
                     withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height = scrollView.frame.size.height / scale;
    zoomRect.size.width  = scrollView.frame.size.width / scale;
    zoomRect.origin.x    = center.x - (zoomRect.size.width / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    return zoomRect;
}

#pragma mark - UIScrollView代理方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

@end

一个需要注意的地方:

需要将图片的view在UIScrollView的代理方法中传递出去

至于这有怎么样的用处,如果有需求需要你截取图片的某一个区域,这时候你就知道有啥用了:)

目录
相关文章
|
11月前
HMI-31-【运动模式】解决音乐模块图片显示问题
上一篇中,我们基本实现了音乐模块的布局显示,但是留了个小尾巴,就是图片显示,这个模块中,图片不是方正的,而是有透视的,但是呢,Qt的图像显示显示,我还没有研究那么深入,所以目前只能是像,但是肯定不是真真的透视。我是利用遮罩来实现的,其实还是平面的图片,仅仅是用了一个透视的图片模版来覆盖一下。
HMI-31-【运动模式】解决音乐模块图片显示问题
|
11月前
|
C++
c++搭建的车道线和路牌的识别系统 使用qt界面
c++搭建的车道线和路牌的识别系统 使用qt界面
48 0
An动画基础之元件的影片剪辑动画与传统补间
An动画基础之元件的影片剪辑动画与传统补间
298 0
An动画基础之元件的影片剪辑动画与传统补间
An动画基础之元件的影片剪辑效果
An动画基础之元件的影片剪辑效果
392 0
An动画基础之元件的影片剪辑效果
|
传感器 缓存 物联网
5_2_1_光照信息屏_软件详解|学习笔记
快速学习5_2_1_光照信息屏_软件详解。
142 0
5_2_1_光照信息屏_软件详解|学习笔记
【3D游戏建模】制作3D水波纹板
说起最近的“网红”,不得不提水波纹板了。本文将分享几种制作水波纹板的方法,希望大家看完能有所收获!
292 0
【3D游戏建模】制作3D水波纹板
|
算法 Java 计算机视觉
模拟油画和铅笔画的滤镜效果
模拟油画和铅笔画的滤镜效果
147 0
模拟油画和铅笔画的滤镜效果
|
图形学
Unity3D 物体运动小游戏后续
上篇链接:https://blog.csdn.net/qq_36171287/article/details/84144912 目录 上篇链接:https://blog.
1390 0
下一篇
无影云桌面