图片的缩放源码与使用

这个是调用图像缩放代码片段。通过[self zoomImageButtonPressed : image];调用缩放图片函数就能够了。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        static NSString *cellIdentifier = @"imageValueCell";
        ImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!imageCell) {
            imageCell = [[ImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier capacity:2 type:ImageCellTypeUneditable];
        }

            __weak typeof(self)weakSelf = self;
            imageCell.addImageAction = ^(ImageControl *control ,NSInteger count) {
                weakSelf.selectedControl = control;
                weakSelf.imageCount ++;
                [weakSelf addImage];

            };

            imageCell.showImageAction = ^(UIImage *image)
            {
                [self zoomImageButtonPressed : image];
            };

            imageCell.deleteImageAction = ^(ImageControl *control ,UIImage *image) {
                //do something maybe
                self.imageCount --;

                NSMutableArray *imageArray = [self.images mutableCopy];

                for (UIImage *subImage in imageArray) {
                    if (subImage == image) {
                        [self.images removeObject:subImage];
                    }
                }
            };

        cell = imageCell;
    return cell;
}

详细的缩放图片页面调用逻辑非常easy就没有必要再抽象了。
- (void)zoomImageButtonPressed : (UIImage *)image
{
if (!image)
{
return;
}
else
{
NSString *aString = @”CExpandPicViewController”;

    CExpandPicViewController *expandPicViewController = [[CExpandPicViewController alloc] initWithNibName:aString bundle:nil];
    expandPicViewController.image = image;

    [self presentViewController:expandPicViewController animated:NO completion:nil];
}

}

CExpandPicViewController.h文件的代码:

#import <UIKit/UIKit.h>

@interface CExpandPicViewController : UIViewController<UIScrollViewDelegate>{
}
@property (nonatomic, strong) IBOutlet UIImageView *imageViewBackground;
@property (nonatomic, strong) IBOutlet UIImageView *imageViewExpandPic;
@property (nonatomic, strong) IBOutlet UIImage *image;
@end

CExpandPicViewController.m文件的代码:

#import "CExpandPicViewController.h"

@interface CExpandPicViewController () <UIScrollViewDelegate>

@property (nonatomic, assign) float imageWith;
@property (nonatomic, assign) float imageHeight;
@property(retain,nonatomic)UIScrollView *scrollerView;
@property(retain,nonatomic)UIImageView *imageView;
@property(retain,nonatomic)UIImageView *imageViewBG;

@end

@implementation CExpandPicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
       [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:NO];
    // Do any additional setup after loading the view from its nib.
    if(_image == nil)
    {
        return;
    }
    float iWidth = _image.size.width;
    float iHeight = _image.size.height;
    float rate = 2.0;
    if((iHeight > WINDOW_HEIGHT) || (iWidth > WINDOW_WIDTH))
    {
        rate = 2.0;
    }
    else if(WINDOW_WIDTH*1000/iWidth >= (WINDOW_HEIGHT)*1000/iHeight)
    {
        rate = WINDOW_HEIGHT/iHeight;
        if(rate < 2.0)
        {
            rate = 2.0;
        }
    }
    else if(WINDOW_WIDTH*1000/iWidth < (WINDOW_HEIGHT)*1000/iHeight)
    {
        rate = WINDOW_WIDTH/iWidth;
        if(rate < 2.0)
        {
            rate = 2.0;
        }
    }

    _imageViewBackground.backgroundColor = [UIColor colorWithHex:0x000000 alpha:1.0];
    _imageViewBG = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)];
    _imageViewBG.backgroundColor = [UIColor colorWithHex:0x000000 alpha:1.0];
    //_imageViewBG.image = _image;
    _imageViewBG.userInteractionEnabled = YES;
    _imageViewBG.hidden = NO;

    _scrollerView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)];
    _scrollerView.delegate=self;
    _scrollerView.minimumZoomScale=0.5f;
    _scrollerView.maximumZoomScale= rate;

    if((iHeight > WINDOW_HEIGHT) || (iWidth > WINDOW_WIDTH))
    {
        if(iWidth * 1000/WINDOW_WIDTH >= iHeight*1000/(WINDOW_HEIGHT))
        {
            iHeight = iHeight*WINDOW_WIDTH/iWidth;
            iWidth = WINDOW_WIDTH;
        }
        else
        {
            iWidth = iWidth * (WINDOW_HEIGHT)/iHeight;
            iHeight = WINDOW_HEIGHT;
        }
    }

    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake((WINDOW_WIDTH - iWidth)/2, (WINDOW_HEIGHT - iHeight)/2, iWidth, iHeight)];


    _imageView.userInteractionEnabled = YES;
    [_imageView setImage:_image];


    [_scrollerView addSubview:_imageViewBG];
    [_scrollerView addSubview:_imageView];

    [self.view addSubview:_scrollerView];



    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackGround)];
    [_imageViewBG addGestureRecognizer:singleTap];

    _imageViewBackground.hidden = NO;
    _imageViewBackground.userInteractionEnabled = YES;

    UITapGestureRecognizer *bGSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackGround)];
    [_imageView addGestureRecognizer:bGSingleTap];

    UITapGestureRecognizer *backGroundSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackGround)];
    [_imageViewBackground addGestureRecognizer:backGroundSingleTap];
}

- (UIStatusBarStyle)preferredStatusBarStyle

{

    return UIStatusBarStyleLightContent;

}

- (BOOL)prefersStatusBarHidden

{

    return NO;

}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    for (id view in [_scrollerView subviews]) {
        if ([view isKindOfClass:[UIImageView class]]) {
            {
                if(_imageView ==  ((UIImageView *)view))
                {
                    return view;
                }

            }
        }
    }
    return  nil;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{

    if((_imageView.frame.size.width <= WINDOW_WIDTH) && (_imageView.frame.size.height <= WINDOW_HEIGHT))
    {
        CGPoint centerPoint = self.view.center;
        _imageView.center = centerPoint;
    }

}

- (void)onClickBackGround
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

}
@end
原文地址:https://www.cnblogs.com/mfmdaoyou/p/6890009.html