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


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

    效果如下:

    源码:

    //
    //  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的代理方法中传递出去

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

     
  • 相关阅读:
    Spring Boot 无侵入式 实现 API 接口统一 JSON 格式返回
    Java8 Stream:2万字20个实例,玩转集合的筛选、归约、分组、聚合
    排名前 16 的 Java 工具类
    万字详解,JDK8 的 Lambda、Stream 和日期的使用详解
    这样规范写代码,同事直呼“666”
    Java多线程高并发学习笔记——阻塞队列
    零散的MySQL基础总是记不住?看这一篇就够了!
    Java 中自定义注解及使用场景
    最简单的6种防止数据重复提交的方法!
    选择数据库联系人【选择之后将不在列表】
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3936453.html
Copyright © 2020-2023  润新知