图片的缩放源代码与使用

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

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


目录
相关文章
|
开发工具 git Perl
Argo CD 入门扫盲使用(二)
Argo CD 入门扫盲使用
412 0
|
JSON 搜索推荐 API
抖音商品详情API接口:获取商品信息的指南
抖音商品详情API接口由抖音开放平台提供,允许第三方应用访问抖音小店的商品数据,包括基本信息、价格、库存及用户评价等。其优势在于数据实时性、自动化处理、市场分析及个性化推荐。通过注册账号、获取API密钥、阅读文档和构建请求,用户可高效获取商品信息,提升运营效率。未来,该接口将在电商领域发挥更大作用。
|
存储 缓存 数据挖掘
阿里云服务器通用算力型u1与经济型e实例对比与常见问题参考
阿里云的通用算力型u1与经济型e实例均以实惠的价格提供云服务,但各有侧重。经济型e实例采用共享模式,适用于个人开发者、学生及小微企业,适合搭建网站、开发测试等轻量级应用;通用算力型u1实例则提供独享资源,更适合对稳定性和性能有一定要求的企业级应用,如中大型网站、数据分析等场景。e实例基于Intel® Xeon® Platinum处理器,提供ESSD Entry云盘,价格亲民;u1实例同样支持ESSD系列云盘,具备更高性价比和稳定算力保障。选择时,个人用户可优先考虑经济型e实例,追求性价比;企业用户则推荐使用通用算力型u1实例,以获得更佳的性能和服务质量保证。
370 4
阿里云服务器通用算力型u1与经济型e实例对比与常见问题参考
|
12月前
|
存储 移动开发 前端开发
HTML基础知识:构建网页的基石
【10月更文挑战第11天】HTML基础知识:构建网页的基石
634 0
|
安全 网络协议
curl使用小记(四)——在多线程中使用的问题总结
curl使用小记(四)——在多线程中使用的问题总结
594 0
|
Java 测试技术
JMeter接口性能测试使用
JMeter接口性能测试使用
153 0
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的博物馆展览与服务一体化平台的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的博物馆展览与服务一体化平台的详细设计和实现(源码+lw+部署文档+讲解等)
248 0
|
数据采集 安全 测试技术
kookeey代理ip适用于那些行业
Kookeey代理IP,以其高效稳定安全特性,成为多行业网络解决方案优选。助力数据采集规避封锁,保障爬虫高效运行;支持广告验证与品牌保护,优化营销策略;服务跨境电商,深入全球市场调研;管理社交媒体多账号,实现地域化精准营销;加强网络安全测试,保护隐私。选择Kookeey,提升工作效率,降低风险成本。
|
弹性计算 监控 关系型数据库
利器解读!Linux 内核调测中最最让开发者头疼的 bug 有解了|龙蜥技术
通过在Anolis 5.10 内核中增强 kfence 的功能,实现了一个线上的、精准的、可定制的内存调试解决方案。
利器解读!Linux 内核调测中最最让开发者头疼的 bug 有解了|龙蜥技术
|
存储 安全
FreeRTOS入门教程(队列的概念及相关函数介绍)
FreeRTOS入门教程(队列的概念及相关函数介绍)
302 0