图片的缩放源代码与使用

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

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


目录
相关文章
|
机器学习/深度学习 监控 Ubuntu
perf性能分析工具使用分享
perf性能分析工具使用分享
3224 0
perf性能分析工具使用分享
|
4月前
|
人工智能 缓存 前端开发
Token计费真相:输入、输出、上下文隐性开销深度解析
多数企业只看懂大模型表面账单,却忽略上下文、系统提示、会话叠加等隐性Token开销。本文拆解Token计费逻辑,揭秘隐形耗损点,给出可落地的降本治理方案。
|
12月前
|
弹性计算 安全 Linux
阿里云服务器购买指南:优惠攻略 + 自定义配置全流程(新手必看)
对于新手来说,购买阿里云服务器不仅要选对配置,更要抓住优惠福利。本文整合了阿里云服务器的两种核心购买方式—— 活动中心优惠购买与 ECS 自定义购买,详细拆解配置选择要点和操作步骤,帮你用最低成本选到合适的服务器,适配网站搭建、应用部署等多种场景。
|
NoSQL 关系型数据库 MySQL
一篇文章带你弄懂SAAS云服务模式
本文详解IAAS、PAAS、SAAS、DAAS四大云服务模式,涵盖基础设施、平台、软件到数据服务的演进逻辑,并深入解析SAAS中的多租户与单租户架构差异,助你全面理解云计算核心概念与应用模式。
1638 11
|
6月前
|
Oracle 关系型数据库 Linux
VirtualBox 4.3.10 虚拟机安装配置教程(Windows XP/Win7 兼容版)
本指南详解VirtualBox 4.3.10在Windows上的安装全流程:含网盘下载地址、管理员运行要点、关键选项勾选(务必保留网络支持)、虚拟网卡驱动安装提示及成功验证方法,步骤清晰,零基础可快速完成部署。(239字)
|
6月前
|
运维 监控 Java
安卓手机群控系统搭建全攻略:ADB+Python实现一台电脑管理几十部手机实操教程
本文围绕安卓手机群控系统的落地实现展开,基于谷歌原生ADB调试协议,结合Python语言完成整套群控框架的代码开发,无需额外专用硬件,详细讲解从环境搭建、设备批量连接、核心触控模块封装、并发批量操作到屏幕投射、设备状态监控的全流程实现方法。文章附带超1700行完整可运行源码,演示如何用一台普通电脑稳定管控几十部安卓设备,适合移动应用测试、批量设备运维场景的开发者参考,所有方案均经过实际多设备环境验证。
|
8月前
|
人工智能 算法 自动驾驶
AI伦理困境——算法偏见、隐私边界与监管博弈
随着AI系统深入渗透到社会运行的各个环节,伦理问题已从学术讨论变为现实危机。
999 0
|
9月前
|
弹性计算 NoSQL 关系型数据库
阿里云 ECS 经济型 e 实例 2 核 2G 99 元一年 详解性能、场景与购买条件
阿里云ECS经济型e实例推出“2核2G,99元/年”特惠活动(至2027.3.31),搭载Intel Xeon Platinum处理器、3Mbps不限流带宽及40GB ESSD系统盘,适合个人开发、学生实验、小微企业官网及轻量测试场景,新老用户实名认证后均可购,限1台/主体。
1511 6
|
11月前
|
人工智能 JSON 自然语言处理
AI解决传统录入效率大问题,我是如何改造了智能采购录入系统
针对采购团队手动整理聊天记录、录入订单效率低、易出错的痛点,我们基于JBoltAI开发了“智能采购录入系统”。通过对接企业微信/微信,自动采集并解析多模态聊天内容(文字、图片、文件、语音),利用AI实现采购信息自动提取、结构化生成订单,并无缝对接现有系统。项目实现录入效率提升70%以上,错误率降至1%以下,支持信息全程追溯,大幅降低采购风险。本文详述架构设计、核心功能实现及关键问题解决方案,为同类场景提供可复用的技术路径与实践参考。
870 3