iOS实现图片的缩放和居中显示

2024-08-28 03:38

本文主要是介绍iOS实现图片的缩放和居中显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

直接上代码

//

//  MoveScaleImageController.h

//  MoveScaleImage

//

//  Created by  on 12-4-24.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "MoveScaleImageView.h"


@interface MoveScaleImageController : UIViewController<UIScrollViewDelegate>{

    UIScrollView *myScrollView;

    UIImageView *myImageView;

}

@property(retain,nonatomic)UIScrollView *myScrollView;

@property(retain,nonatomic)UIImageView *myImageView;


@end


 

//

//  MoveScaleImageController.m

//  MoveScaleImage

//

//  Created by  on 12-4-24.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import "MoveScaleImageController.h"


@interface MoveScaleImageController ()


@end


@implementation MoveScaleImageController

@synthesize myScrollView;

@synthesize myImageView;


-(void)dealloc{

    [myScrollView release];

    [myImageView release];

    [super dealloc];

}


-(void)loadView{

    [super loadView];

    self.view.backgroundColor = [UIColor lightGrayColor];

    

//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(110, 200, 100, 50)];

//    [btn setFrame:CGRectMake(110, 200, 100, 40)];

    [btn setBackgroundColor:[UIColor whiteColor]];

    [btn setTitle:@"点击查看图片" forState:UIControlStateNormal];

    [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];

    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    [btn release];

    

    //下面是我要剪切区域的覆盖层

//    if(self.centerOverLayView==nil)

//    {

//        UIView *centerView=[[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 210)];

//        self.centerOverLayView=centerView;

//        [centerView release];

//    }

//    self.centerOverLayView.backgroundColor=[UIColor clearColor];

//    self.centerOverLayView.layer.borderColor=[UIColor orangeColor].CGColor;

//    self.centerOverLayView.layer.borderWidth=2.0;

//    [self.view addSubview:self.centerOverLayView];

    

}


-(void)clickEvent:(id)sender{

    NSLog(@"***********clickeventad");

    myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    if(self.myScrollView==nil)

    {

        UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

        self.myScrollView=scrollView;

        [scrollView release];

    }

    self.myScrollView.backgroundColor=[UIColor blueColor];

    self.myScrollView.delegate=self;

    self.myScrollView.multipleTouchEnabled=YES;

    self.myScrollView.minimumZoomScale=1.0;

    self.myScrollView.maximumZoomScale=10.0;

    [self.view addSubview:self.myScrollView];

    

    UIImage *_image = [UIImage imageNamed:@"image.jpg"];

    CGFloat imageView_X = (_image.size.width > self.view.frame.size.width) ? self.view.frame.size.width : _image.size.width;

    CGFloat imageView_Y;

    CGFloat origin;

    if(_image.size.width > self.view.frame.size.width){

        origin = self.view.frame.size.width/_image.size.width;

        imageView_Y = _image.size.height*origin;

    }

    myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-imageView_X)/2, (self.view.frame.size.height-imageView_Y)/2, imageView_X, imageView_Y)];

   

    if(self.myImageView==nil)

    {

        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

        self.myImageView=imageView;

        [imageView release];

    }

   

//    [myImageView setImage:_image];

    

    UIImage *originImage=[[UIImage alloc]initWithCGImage:_image.CGImage];

    [myImageView setImage:originImage];

//    [myImageView setFrame:CGRectMake(0, 0, _image.size.width, _image.size.height)];

    

    [self.myScrollView addSubview:self.myImageView];

    

    UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];

    [closeBtn setBackgroundColor:[UIColor redColor]];

    [closeBtn setAlpha:0.5];

    [closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:closeBtn];

    [closeBtn release];

    

//    UIView *backView = [[UIView alloc] initWithFrame:CGRectInset(self.view.frame, 5, 5)];

//    backView.alpha = 0.5;

//    backView.backgroundColor = [UIColor blackColor];

    [self.view addSubview:backView];

//    

//    UIImage* image=[UIImage imageNamed:@"image.jpg"];

//    MoveScaleImageView*fileContent = [[MoveScaleImageView alloc]initWithFrame:CGRectMake(0, 44, 320, 436)];

//    [fileContent setImage:image];

//    

    [backView addSubview:fileContent];

//    [self.view addSubview:fileContent];

//    

//    [backView release];

//    [fileContent release];

}


-(void)closeEvent:(id)sender{

    [self.myImageView setHidden:YES];

    [self.myScrollView setHidden:YES];

}


#pragma mark UIScrollView delegate methods

//实现图片的缩放

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

    NSLog(@"**************viewForZoomingInScrollView");

    return self.myImageView;

}

//实现图片在缩放过程中居中

- (void)scrollViewDidZoom:(UIScrollView *)scrollView

{

    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;

    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;

    self.myImageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


@end

这篇关于iOS实现图片的缩放和居中显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1113623

相关文章

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

基于SpringBoot实现文件秒传功能

《基于SpringBoot实现文件秒传功能》在开发Web应用时,文件上传是一个常见需求,然而,当用户需要上传大文件或相同文件多次时,会造成带宽浪费和服务器存储冗余,此时可以使用文件秒传技术通过识别重复... 目录前言文件秒传原理代码实现1. 创建项目基础结构2. 创建上传存储代码3. 创建Result类4.

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分