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& ...
随机推荐
- Android 高级UI设计笔记18:实现圆角图片
1. 下面我们经常在APP中看到的圆角图片,如下: 再比如:微信聊天会话列表的头像是圆角的. 2. 下面分析一个Github的经典: (1)Github库地址: https://github.com/ ...
- 1. windows下作为应用程序启动apache的方法
1. 具体步骤如下:(文章末尾附加:Apache 2.2.17下载路径) 步骤一 :Cmd打开命令行窗口,切换到apache安装目录下 cd C:\MAS\TRSMAS\win31\apache\b ...
- VA中修改函数注释
在VA中修改对应的函数和文件注释,可以使用VA自动产生函数和文件头注释 //************************************************************** ...
- 修改tomcat浏览器地址栏图标
1.准备一张jpg格式的图片 2.去百度:ico图标在线制作(快速入口:http://www.faviconico.org/) 3.将生成的ico图标复制到tomcat的webapps下的ROOT项目 ...
- 转:微信Android客户端架构演进之路
转自: http://www.infoq.com/cn/articles/wechat-android-app-architecture 微信Android客户端架构演进之路 作者 赵原 发布于 20 ...
- php学习笔记7--php中的数组
php中的数组 1.数组的定义:显式方式:$arr = array(1,2,3,4,5); $arr1 = array('name'=>'dqrcsc','age'=>'24');隐式方式 ...
- sql 选取每个分组中的第一条数据
--1.创建测试表Create Table #Order1( OrderName varchar(50), RequestDate datetime, OrderCount int)-- 插入测试数据 ...
- CCNA的RIP路由学习
rip(routing infomation protocol,路由信息协议) ,是一个纯粹的距离矢量路由选择协议,RIP每隔30s就将自己完整的路由选择表从所有激活的接口上送出.RIP只将跳计数作为 ...
- 解决windows端口被占用
1, Cmd输入命令:netstat –ano|findstr “端口号” ,如netstat –ano|findstr “8080” 记下PID,最后一行为PID,这里为396 2,Cmd输 ...
- throws与throw的对比
1.throws关键字通常被应用在声明方法时,用来指定可能抛出的异常.多个异常可以使用逗号隔开.当在主函数中调用该方法时,如果发生异常,就会将异常抛给指定异常对象.如下面例子所示:public cla ...