您的位置:首页 > 运维架构

NSOperation下载图片,图片可平移伸缩,然后裁剪图片

2016-02-21 00:22 302 查看
//

//  DownloadImageOperation.h

//  tst

//

//  Created by zmx on 16/2/20.

//  Copyright © 2016年 zmx. All rights reserved.

//

#import <UIKit/UIKit.h>

typedef void(^FinishBlock)(UIImage *image);

@interface DownloadImageOperation :
NSOperation

@property (nonatomic,copy)NSString *urlStr;

@property (nonatomic,copy)FinishBlock finishBlock;

@end

//

//  DownloadImageOperation.m

//  tst

//

//  Created by zmx on 16/2/20.

//  Copyright © 2016年 zmx. All rights reserved.

//

#import "DownloadImageOperation.h"

@implementation DownloadImageOperation

- (void)main {

    @autoreleasepool {

        NSURL *url = [NSURLURLWithString:self.urlStr];

        NSData *data = [NSDatadataWithContentsOfURL:url];

        UIImage *image = [UIImageimageWithData:data];

        

        dispatch_async(dispatch_get_main_queue(), ^{

            if (self.finishBlock) {

                self.finishBlock(image);

            }

        });

    }

}

@end

4000

//

//  ViewController.m

//  tst

//

//  Created by zmx on 16/2/17.

//  Copyright © 2016年 zmx. All rights reserved.

//

#import "ViewController.h"

#import "DownloadImageOperation.h"

#import "UIView+Ext.h"

@interface
ViewController ()

@property (nonatomic,weak)UIImageView *imageView;

@property (nonatomic,strong)NSOperationQueue *queue;

@property (nonatomic,assign)CGPoint currentPoint;

@property (nonatomic,assign)CGFloat currentScale;

@property (nonatomic,assign)CGFloat imageScale;

@end

@implementation ViewController

- (void)save:(UIButton *)sender {

    //获取imageView相对image的放大倍数

    CGFloat scale =
self.imageView.w /self.imageView.image.size.width;

    

    CGFloat x = -
self.imageView.x / scale;

    CGFloat y = (self.imageView.h <self.view.h
-200 ? 0 :
100 - self.imageView.y) / scale;

    CGFloat w = self.view.w / scale;

    CGFloat h = (self.imageView.h <self.view.h
-200 ? self.imageView.h :self.view.h
-200) / scale;

    CGImageRef imageRef =CGImageCreateWithImageInRect(self.imageView.image.CGImage,CGRectMake(x,
y, w, h));

    UIImage *image = [UIImageimageWithCGImage:imageRef];

    

    NSData *data =UIImagePNGRepresentation(image);

    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES)firstObject]
stringByAppendingPathComponent:@"image.jpg"];

    [data writeToFile:pathatomically:YES];

}

- (NSOperationQueue *)queue {

    if (_queue ==nil) {

        _queue = [[NSOperationQueuealloc]
init];

    }

    return _queue;

}

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self setupUI];

    

    [selfdownloadImage];

    

    [selfsetupGestureRecognizer];

}

- (void)setupUI {

    UIImageView *imageView = [[UIImageViewalloc]
init];

    self.imageView = imageView;

    [self.viewaddSubview:self.imageView];

    

    UIView *topView = [[UIViewalloc]
initWithFrame:CGRectMake(0,0,
self.view.w,100)];

    [self.viewaddSubview:topView];

    topView.backgroundColor = [UIColordarkGrayColor];

    topView.alpha =
0.6;

    

    UILabel *label = [[UILabelalloc]
init];

    [topView addSubview:label];

    label.text =@"移动并伸缩图片至理想画面";

    label.textColor = [UIColorlightGrayColor];

    label.font = [UIFontsystemFontOfSize:16];

    CGSize labelSize = [label.textsizeWithAttributes:@{NSFontAttributeName: label.font}];

    label.bounds = (CGRect){CGPointZero, labelSize};

    label.center =
CGPointMake(topView.w *
0.5, topView.h *
0.5);

    

    UIView *bottomView = [[UIViewalloc]
initWithFrame:CGRectMake(0,self.view.h -100,
self.view.w,100)];

    [self.viewaddSubview:bottomView];

    bottomView.backgroundColor = [UIColordarkGrayColor];

    bottomView.alpha =
0.6;

    

    UIButton *saveButton = [[UIButtonalloc]
init];

    [self.viewaddSubview:saveButton];

    [saveButton setTitle:@"保存"forState:UIControlStateNormal];

    saveButton.titleLabel.font = [UIFontsystemFontOfSize:15];

    CGSize titleSize = [saveButton.currentTitlesizeWithAttributes:@{NSFontAttributeName:
saveButton.titleLabel.font}];

    CGSize buttonSize =
CGSizeMake(titleSize.width +
10, titleSize.height +
10);

    saveButton.frame =
CGRectMake(self.view.w - buttonSize.width -10,
20, buttonSize.width, buttonSize.height);

    [saveButton addTarget:selfaction:@selector(save:)forControlEvents:UIControlEventTouchUpInside];

}

- (void)downloadImage {

    DownloadOperation *operation = [[DownloadOperationalloc]
init];

    operation.urlStr =@"http://imgs.it007.com/data/attachment/forum/201511/26/174705khwuujwn82k2peh7.jpg.thumb.jpg";

    

    __weak typeof(self) weakSelf =self;

    operation.finishBlock = ^(UIImage *image) {

        weakSelf.imageView.image = image;

        

        CGFloat imageW =
self.view.w;

        CGFloat imageH = imageW / image.size.width * image.size.height;

        weakSelf.imageView.bounds =CGRectMake(0,0, imageW, imageH);

        

        weakSelf.imageView.center =self.view.center;

        

        weakSelf.imageScale =
1;

    };

    

    [self.queueaddOperation:operation];

}

- (void)setupGestureRecognizer {

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]
initWithTarget:selfaction:@selector(onPan:)];

    [self.viewaddGestureRecognizer:pan];

    

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]
initWithTarget:selfaction:@selector(onPinch:)];

    [self.viewaddGestureRecognizer:pinch];

}

- (void)onPan:(UIPanGestureRecognizer *)pan {

    if (pan.state == UIGestureRecognizerStateBegan)
{

        self.currentPoint = CGPointZero;

    }

    

    CGPoint trans = [pan translationInView:self.view];

    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform,
trans.x - self.currentPoint.x,
trans.y - self.currentPoint.y);

    self.currentPoint = trans;

    

    if (pan.state == UIGestureRecognizerStateEnded ||
pan.state == UIGestureRecognizerStateCancelled) {

        

        //  进行伸缩变换后,位移量要除以伸缩比例,否则位移效果有偏差

        CGFloat dRight = self.view.w - CGRectGetMaxX(self.imageView.frame);

        if (dRight > 0) {

            [UIView animateWithDuration:0.5 animations:^{

                self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform,
dRight /self.imageScale, 0);

            }];

        }

        

        CGFloat dLeft = - self.imageView.x;

        if (dLeft < 0) {

            [UIView animateWithDuration:0.5 animations:^{

                self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform,
dLeft /self.imageScale, 0);

            }];

        }

        

        //如果图片高度在白色区域范围内,不能垂直平移

        if (self.imageView.h < self.view.h - 200)
{

            [UIView animateWithDuration:0.5 animations:^{

                self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 0,
- trans.y / self.imageScale);

            }];

        } else {

            CGFloat dBottom = self.view.h - CGRectGetMaxY(self.imageView.frame)
- 100;

            if (dBottom > 0) {

                [UIView animateWithDuration:0.5 animations:^{

                    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 0,
dBottom / self.imageScale);

                }];

            }

            

            CGFloat dTop = 100 - self.imageView.y;

            if (dTop < 0) {

                [UIView animateWithDuration:0.5 animations:^{

                    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 0,
dTop / self.imageScale);

                }];

            }

        }

    }

}

- (void)onPinch:(UIPinchGestureRecognizer *)pinch {

    if (pinch.state == UIGestureRecognizerStateBegan)
{

        self.currentScale = 1;

    }

    

    //伸缩比例用减法计算,相比用除法可以获得同等缩放手势更小的伸缩范围

    CGFloat scale = pinch.scale - self.currentScale + 1.0;

    self.imageView.transform = CGAffineTransformScale(self.imageView.transform,
scale, scale);

    self.currentScale = pinch.scale;

    self.imageScale *= scale;

    

    if (pinch.state == UIGestureRecognizerStateEnded ||
pinch.state == UIGestureRecognizerStateCancelled) {

        if (self.imageScale > 2) {

            self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 2 / self.imageScale, 2 / self.imageScale);

            self.imageScale = 2;

        }

        

        if (self.imageScale < 1) {

            self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 1 / self.imageScale, 1 / self.imageScale);

            self.imageScale = 1;

        }

        

        //伸缩后图片上下左右留白的处理

        CGFloat dRight = self.view.w - CGRectGetMaxX(self.imageView.frame);

        if (dRight > 0) {

            [UIView animateWithDuration:0.5 animations:^{

                self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform,
dRight /self.imageScale, 0);

            }];

        }

        

        CGFloat dLeft = - self.imageView.x;

        if (dLeft < 0) {

            [UIView animateWithDuration:0.5 animations:^{

                self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform,
dLeft /self.imageScale, 0);

            }];

        }

        

        //如果图片高度在白色区域范围内,让图片垂直居中

        if (self.imageView.h < self.view.h - 200)
{

            [UIView animateWithDuration:0.5 animations:^{

                CGFloat y1 = self.imageView.y;

                CGFloat y2 = (self.view.h - self.imageView.h)
* 0.5;

                self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 0,
(y2 - y1) / self.imageScale);

            }];

        } else {

            CGFloat dBottom = self.view.h - CGRectGetMaxY(self.imageView.frame)
- 100;

            if (dBottom > 0) {

                [UIView animateWithDuration:0.5 animations:^{

                    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 0,
dBottom / self.imageScale);

                }];

            }

            

            CGFloat dTop = 100 - self.imageView.y;

            if (dTop < 0) {

                [UIView animateWithDuration:0.5 animations:^{

                    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 0,
dTop / self.imageScale);

                }];

            }

        }

    }

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息