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
转载自:http://blog.sina.com.cn/s/blog_9c3c519b0100za22.html
iOS实现图片的缩放和居中显示的更多相关文章
- ios 改变图片大小缩放方法
http://www.cnblogs.com/zhangdadi/archive/2012/11/17/2774919.html http://bbs.csdn.net/topics/39089858 ...
- NPOI 图片在单元格等比缩放且居中显示
NPOI导出的图片默认是在单元格左上方,这使得图片在单元格显示得很难看.居中,且等比缩放,才是图片在单元格上的完美展示. /// <summary> /// 图片在单元格等比缩放居中显示 ...
- CSS图片Img等比例缩放且居中显示
常用来做图片放大显示的遮罩层imgScale HTML <div id="imgScale" > <img src=""> </d ...
- 用<center/>标签实现markdown 图片文字等内容居中显示
markdown中,文字居中的方式是借助了html标签<center></center>的支持 示例  { var target_w ...
- 纯JS实现图片预览与等比例缩放和居中
最近做项目时有一个需求,广告位图片上传时要预览,并且要等比例缩放和居中.已经保存的广告位图片显示时也要等比例缩放和居中.我使用了下面的代码实现,不过可能有一些小问题. <!DOCTYPE HTM ...
- css 图片内容在不同分辨率下居中显示(演示的图片宽度是1920px,当图片宽度大于显示屏的宽度时)
1.img 图片内容在不同分辨率下居中显示(如果隐藏多余,在img外面套一个div 设定overflow: hidden.div的大小就是img显示区域的大小) <!DOCTYPE html& ...
随机推荐
- 1.4.2 solr字段类型--(1.4.2.4)使用Dates(日期)
1.4.2 solr字段类型 (1.4.2.1) 字段类型定义和字段类型属性. (1.4.2.2) solr附带的字段类型 (1.4.2.3) 使用货币和汇率 (1.4.2.4) 使用Dates(日期 ...
- Android 高级UI设计笔记16:ViewStub的应用
1. ViewStub 在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局. 那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为 ...
- Android 珍藏(二)
一.如何控制Android LED等?(设置NotificationManager的一些参数) 代码如下: final int ID_LED=19871103; NotificationManage ...
- IOS开发UI篇之tableView 的用法详解
1.我们知道tableView是IOS中的高级视图,其继承与ScrollView,故我们知道他有具有ScrollView的所有功能.而且还扩展了许多.当然在这里就不一一介绍了. 2.tableView ...
- js自动刷新页面代码
<script language="JavaScript">function myrefresh(){window.location.reload();}setTime ...
- amoeba实现MySQL读写分离
amoeba实现MySQL读写分离 准备环境:主机A和主机B作主从配置,IP地址为192.168.131.129和192.168.131.130,主机C作为中间件,也就是作为代理服务器,IP地址为19 ...
- 【递归】油桶问题dp
问题 : [递归]油桶问题 题目描述 楚继光扬扬得意道:“当日华山论剑,先是他用黯然销魂掌破了我的七十二路空明拳,然后我改打降龙十八掌,却不防他伸开食指和中指,竟是六脉神剑,又胜我一筹.可见天下武学彼 ...
- Oracle数据库作业-3 查询
1. 查询Student表中的所有记录的Sname.Ssex和Class列.
- 默认样式重置 (css reset)
body,p,h1,h2,h3,h4,h5,h6,dl,dd,t{margin:0; font-size:12px;/* font-family:XX; */} ol,ul{list-style:no ...
- Part 45 to 47 Talking about Enums in C#
Part 45 C# Tutorial Why Enums Enums are strongly typed constants. If a program uses set of integ ...