直接上代码

//

//  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实现图片的缩放和居中显示的更多相关文章

  1. ios 改变图片大小缩放方法

    http://www.cnblogs.com/zhangdadi/archive/2012/11/17/2774919.html http://bbs.csdn.net/topics/39089858 ...

  2. NPOI 图片在单元格等比缩放且居中显示

    NPOI导出的图片默认是在单元格左上方,这使得图片在单元格显示得很难看.居中,且等比缩放,才是图片在单元格上的完美展示. /// <summary> /// 图片在单元格等比缩放居中显示 ...

  3. CSS图片Img等比例缩放且居中显示

    常用来做图片放大显示的遮罩层imgScale HTML <div id="imgScale" > <img src=""> </d ...

  4. 用<center/>标签实现markdown 图片文字等内容居中显示

    markdown中,文字居中的方式是借助了html标签<center></center>的支持 示例 ![](https://img2018.cnblogs.com/blog/ ...

  5. 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示

    我们都知道在ios中,每一个UIImageView都有他的frame大小,但是如果图片的大小和这个frame的大小不符合的时候会怎么样呢?在默认情况,图片会被压缩或者拉伸以填满整个区域. 通过查看UI ...

  6. Android ImageView的几种对图片的缩放处理 解决imageview放大图片后失真问题解决办法

    我的解决办法: 1 首先设置android:layout_width=”wrap_content”和android:layout_height=”wrap_content”,否则你按比例缩放后的图片放 ...

  7. js控制 固定框架内图片 按比例显示 以及 占满框架 居中显示

    js控制 固定框架内图片 等比例显示 以及 占满框架 纵横居中显示 通过设置 js函数 fitDiv里面var fit的值就好 function fitDiv (obj) { var target_w ...

  8. 纯JS实现图片预览与等比例缩放和居中

    最近做项目时有一个需求,广告位图片上传时要预览,并且要等比例缩放和居中.已经保存的广告位图片显示时也要等比例缩放和居中.我使用了下面的代码实现,不过可能有一些小问题. <!DOCTYPE HTM ...

  9. css 图片内容在不同分辨率下居中显示(演示的图片宽度是1920px,当图片宽度大于显示屏的宽度时)

    1.img 图片内容在不同分辨率下居中显示(如果隐藏多余,在img外面套一个div  设定overflow: hidden.div的大小就是img显示区域的大小) <!DOCTYPE html& ...

随机推荐

  1. iOS 抓取 UIwebview 上 所有 图片 并进行滚动播放

    关于在UIwebview上添加滚动图片 两种滚动手势会混淆,应为webview有webview.scrollview的属性 故参照昨天的随笔 scrollview嵌套解决方案. 本篇随笔主要讲循环使用 ...

  2. WebStorm配置(2016/11/18更新)

    目录: 1.主题设置 2.模板设置 3.代码段设置 4.快捷键设置 5.显示行号+自动换行 6.配置github 7.常用快捷键 8.软件下载(破解版及汉化包) 1.主题设置 1)ctrl+alt+s ...

  3. 如何利用OCS存取PHP session全局变量

    如何利用OCS存取PHP session全局变量 阿里云技术团队:余汶龙   一.场景介绍 用户在利用PHP搭建网站时,会把一些信息存放在$_SESSION全局变量里,可以很方便的存取.在PHP的in ...

  4. 【Shell脚本学习16】Shell if else语句

    if 语句通过关系运算符判断表达式的真假来决定执行哪个分支.Shell 有三种 if ... else 语句: if ... fi 语句: if ... else ... fi 语句: if ... ...

  5. 【Shell脚本学习12】Shell字符串

    字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似. 单引号 str='this ...

  6. oracle日志总结

    ①. Oracle日志分类: Alert log files--警报日志 , redo log 重做日志(记录数据库的更改,Trace files--跟踪日志(用户和进程) Oracle的重做日志(r ...

  7. 【原创】C++链表如何像Python List一样支持多种数据类型

    用过Python的码友都知道,Python中List支持多种数据类型,如下面代码所示链表li内的数据类型可以是整数,同时也可以是字符串,当然也可以是其他数据类型. 1: >>> li ...

  8. sql server小技巧-自动添加时间与主键自增长

    在敲机房收费系统的时候,遇到添加时间的时候总是通过vb端调用当前时间再添到sql server中,期间还有时因为添加时间格式的不统一导致一些小问题,现在才知道原来是自己孤陋寡闻,sql server ...

  9. 系统磁盘空间/dev/xvda1占满原因分析

    由于项目原因需要定期检查磁盘空间占用情况,常用检查命令如下: 1.查看磁盘空间大小 df -lh 2.查看对应文件大小 du --max-depth=1 -h / 于一日发现在使用Flume + Ka ...

  10. web前端开发学习指南(大群主推荐)

    http://www.ituring.com.cn/book/1140 http://www.ituring.com.cn/book/1361