图片的缩放源代码与使用

简介: 图片的缩放源代码与使用

这个是调用图像缩放代码片段,通过[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;
}

具体的缩放图片页面调用逻辑很简单就没有必要再抽象了。

- (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


目录
相关文章
|
1月前
|
Android开发
Android Studio入门之图像显示解析及实战(附源码 超详细必看)(包括图像视图、图像按钮、同时展示文本与图像)
Android Studio入门之图像显示解析及实战(附源码 超详细必看)(包括图像视图、图像按钮、同时展示文本与图像)
126 1
|
1月前
|
Dart
Flutter 学习之图片的选择、裁切、保存
Flutter 学习之图片的选择、裁切、保存 在Flutter中,我们可以通过调用系统的图片选择器来选择一张图片,也可以通过使用插件来实现图片的裁切和保存。
143 0
图片和文件预览组件(部分源码),可拖动,缩小,放大。 #41
图片和文件预览组件(部分源码),可拖动,缩小,放大。 #41
104 0
【图片操作】给图片添加滤镜
现在我们都喜欢给图片添加滤镜,现在很多相机也自带了许多滤镜。我们可以在拍照的时候选择需要的滤镜。但是有时候我们需要给大量图片添加同样的滤镜,这个时候手动添加就非常麻烦了。为了方便,我们可以使用程序来帮我们完成添加滤镜的操作。
206 0
|
Java API Maven
一行代码搞定图片缩略图处理
不知道大家现在工作中还有没有使用过Java处理图片的。强哥在大学毕业后,从事服务端WEB开发,就很少接触图片处理。有接触图片的,大多也就是图片的上传下载。所以,对Java处理图片相关的技术也都没怎么接触。
一行代码搞定图片缩略图处理
|
人工智能 前端开发
Photoshop - 如何用 PS 合成一张 Sprite 图(雪碧图)
Photoshop - 如何用 PS 合成一张 Sprite 图(雪碧图)
348 0
Photoshop - 如何用 PS 合成一张 Sprite 图(雪碧图)
|
算法 图形学
【Unity3D Shader】学习笔记-图片滤镜①
效果和上面的比较相似,Photoshop CS图像黑白调整功能的计算公式为: gray= (max - mid) * ratio_max + (mid - min) * ratio_max_mid + min 公式中:gray为像素灰度值,max、mid和min分别为图像像素R、G、B分量颜色的最大值、中间值和最小值,ratio_max为max所代表的分量颜色(单色)比率,ratio_max_mid则为max与mid两种分量颜色所形成的复色比率。 默认的单色及复色比率为:
855 0
【Unity3D Shader】学习笔记-图片滤镜①
|
C++
duilib corner属性的贴图技巧——让图片自动贴到控件的的某一边或者一角并自适应控件的大小
转载请说明原出处,谢谢~~          Duilib给控件贴图功能可以附带多个属性,各个属性的配合可以达到许多效果。以下是duilib支持的所有贴图属性: 贴图描述:          Duilib的表现力丰富很大程度上得益于贴图描述的简单强大。
1736 0
|
Web App开发 移动开发 前端开发
把canvas标签里的图像下载成本地图片文件
把canvas标签里的图像下载成本地图片文件
504 0
把canvas标签里的图像下载成本地图片文件
|
移动开发 计算机视觉 Python
把图片按照指定大小剪裁,不够的地方加黑边
1 # -*- coding: utf-8 -*- 2 3 import os 4 import sys 5 import numpy as np 6 import cv2 7 8 IMAGE_SIZE = 224 9 10 11 # 按照指定图像大小调整尺寸 12 de...
1224 0