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.1.Documents,Fields和Schema概述
Documents,Fields和Schema概述 solr的基本前提是非常简单,你可以给它很多信息,然后可以向它提出问题,获取你想要的问题的信息.所有信息输入的地方就叫做索引或者更新.当你提出问题时 ...
- 自定义uitableviewcell通过加上滑动手势进行删除对应的行。PS:用代理来实现
#import <UIKit/UIKit.h> @class ZSDCustomCell; //协议 @protocol ZSDCustomCellDelegate <NSObjec ...
- 『创意欣赏』30幅逼真的 3D 虚拟现实环境呈现
又到周末了,给大家分享30幅漂亮的 3D 虚拟现实环境呈现,放松一下.这些创造性的场景都是通过 3D 图形设计软件,结合三维现实环境渲染制作出来的.一起欣赏:) 您可能感兴趣的相关文章 20幅温馨浪漫 ...
- Android(java)学习笔记264:Android下的属性动画高级用法(Property Animation)
1. 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法,当然也是最常用的一些用法,这些用法足以覆盖我们平时大多情况下的动画需求了.但是,正如上篇文章当中所说到的,属性动画对补间动画 ...
- 重构20-Extract Subclass(提取父类)
当一个类中的某些方法并不是面向所有的类时,可以使用该重构将其迁移到子类中.我这里举的例子十分简单,它包含一个Registration类,该类处理与学生注册课程相关的所有信息. public class ...
- 【AR】Vuforia App key is missing.Please get a valid key
在跑Vuforia 的sample android app 的时候报了下面这个错,找了半天才找到解决方法: "Vuforia App key is missing. Please get a ...
- 504 Gateway Time-out 和 502 Bad Gateway相关处理
若报:504 Gateway Time-out则与nginx有关 解决方案: #vim nginx.conf 添加以下代码: http{ fastcgi_connect_timeout 300; fa ...
- Table of Contents - Apache Commons
Apache Commons 简述 CLI Usage of CLI Option Properties Codec 常见的编码解码 Compress Configuration2 Quick sta ...
- 【CSS3】---结构性伪类选择器-root+not+empty+target
结构性伪类选择器—root :root选择器,从字面上我们就可以很清楚的理解是根选择器,他的意思就是匹配元素E所在文档的根元素.在HTML文档中,根元素始终是<html>. 示例演示: 通 ...
- Houdini 13在Ubuntu系统下流畅运行、不崩溃
至尊影视特效软件Houdini FX,当前最新版是13.0.547,经过试用在Ubuntu系统下可以完美运行,目前为止还没出现过崩溃的情况,之前在windows下使用Houdini 13简直就是噩梦, ...