直接上代码

//

//  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. Android 高级UI设计笔记18:实现圆角图片

    1. 下面我们经常在APP中看到的圆角图片,如下: 再比如:微信聊天会话列表的头像是圆角的. 2. 下面分析一个Github的经典: (1)Github库地址: https://github.com/ ...

  2. 1. windows下作为应用程序启动apache的方法

    1. 具体步骤如下:(文章末尾附加:Apache 2.2.17下载路径) 步骤一 :Cmd打开命令行窗口,切换到apache安装目录下 cd  C:\MAS\TRSMAS\win31\apache\b ...

  3. VA中修改函数注释

    在VA中修改对应的函数和文件注释,可以使用VA自动产生函数和文件头注释 //************************************************************** ...

  4. 修改tomcat浏览器地址栏图标

    1.准备一张jpg格式的图片 2.去百度:ico图标在线制作(快速入口:http://www.faviconico.org/) 3.将生成的ico图标复制到tomcat的webapps下的ROOT项目 ...

  5. 转:微信Android客户端架构演进之路

    转自: http://www.infoq.com/cn/articles/wechat-android-app-architecture 微信Android客户端架构演进之路 作者 赵原 发布于 20 ...

  6. php学习笔记7--php中的数组

    php中的数组 1.数组的定义:显式方式:$arr = array(1,2,3,4,5); $arr1 = array('name'=>'dqrcsc','age'=>'24');隐式方式 ...

  7. sql 选取每个分组中的第一条数据

    --1.创建测试表Create Table #Order1( OrderName varchar(50), RequestDate datetime, OrderCount int)-- 插入测试数据 ...

  8. CCNA的RIP路由学习

    rip(routing infomation protocol,路由信息协议) ,是一个纯粹的距离矢量路由选择协议,RIP每隔30s就将自己完整的路由选择表从所有激活的接口上送出.RIP只将跳计数作为 ...

  9. 解决windows端口被占用

    1, Cmd输入命令:netstat  –ano|findstr  “端口号” ,如netstat  –ano|findstr  “8080” 记下PID,最后一行为PID,这里为396 2,Cmd输 ...

  10. throws与throw的对比

    1.throws关键字通常被应用在声明方法时,用来指定可能抛出的异常.多个异常可以使用逗号隔开.当在主函数中调用该方法时,如果发生异常,就会将异常抛给指定异常对象.如下面例子所示:public cla ...