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& ...
随机推荐
- [Java,JavaEE] 最常用的Java库一览
引用自:http://www.importnew.com/7530.html 本文由 ImportNew - 邢 敏 翻译自 programcreek.欢迎加入Java小组.转载请参见文章末尾的要求. ...
- 【IT名人堂】何云飞:阿里云数据库的架构演进之路
[IT名人堂]何云飞:阿里云数据库的架构演进之路 原文转载自:IT168 如果说淘宝革了零售的命,那么DT革了企业IT消费的命.在阿里巴巴看来,DT时代,企业IT消费的模式变成了“云服务+数据”, ...
- 【Mood-20】滴滤咖啡做法 IT工程师加班必备 更健康的coffee 项目经理加班密鉴
Drip Coffee
- linux_过程问题记录
常见问题1:-bash: rz: command not found 解决: 安装lrzsz: 解决命令:yum -y install lrzsz 常见问题2:linux 解压乱码 解决11.到htt ...
- CSS属性(常用的属性)
CSS属性(常用的属性)http://www.w3school.com.cn/cssref/index.asp 一:文本与字体属性 1.字体属性 (1):font-size:字体的大小(例如:font ...
- linux-``反引号
反引号`` 这个东西的用法,我百度了一下,和$()是一样的.在执行一条命令时,会先将其中的 ``,或者是$() 中的语句当作命令执行一遍,再将结果加入到原命令中重新执行,例如:echo `ls`会先执 ...
- 如何导入hadoop源码到eclipse
需要进一步学习hadoop.需要看看内部源码实现,因此需要将hadoop源码导入都eclipse中. 简单总结一下,具体步骤如下: 首先确保已经安装了git.maven3.protobuf2.5.如果 ...
- Table of Contents - Tomcat
Manager 持久化 Session JNDI Resources JNDI 配置 JDBC DataSources DBCP 配置 Realm Configuration 设置 HTTP 基本认证 ...
- 获取屏幕中某个点的RGB值与CAD屏幕像素值
'获取CAD屏幕像素的比值 Function ViewScreen() As Double Dim ScreenSize As Variant ScreenSize = ThisDrawing.Get ...
- Stack Overflow 2016最新架构探秘
这篇文章主要揭秘 Stack Overflow 截止到 2016 年的技术架构. 首先给出一个直观的数据,让大家有个初步的印象. 相比于 2013 年 11 月,Stack Overflow 在 20 ...