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

时间: 2024-11-02 06:17:03

iOS实现图片的缩放和居中显示的相关文章

图片在容器充满居中显示

效果图: 源码如下: <!DOCTYPE html> <html>     <head>         <meta charset="UTF-8">         <title>图片在容器充满居中显示</title>         <style type="text/css">             .container{                 width: 100

ios 改变图片大小缩放方法

http://www.cnblogs.com/zhangdadi/archive/2012/11/17/2774919.html http://bbs.csdn.net/topics/390898581 ios 改变图片大小缩放方法 -(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size{    UIGraphicsBeginImageContext(size);  //size 为CGSize类型,即你所需要的图片尺寸

NPOI随笔——图片在单元格等比缩放且居中显示

NPOI导出的图片默认是在单元格左上方,这使得图片在单元格显示得很难看.居中,且等比缩放,才是图片在单元格上的完美展示. /// <summary> /// 图片在单元格等比缩放居中显示 /// </summary> /// <param name="cell">单元格</param> /// <param name="value">图片二进制流</param> private void Cel

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

NPOI导出的图片默认是在单元格左上方,这使得图片在单元格显示得很难看.居中,且等比缩放,才是图片在单元格上的完美展示. /// <summary> /// 图片在单元格等比缩放居中显示 /// </summary> /// <param name="cell">单元格</param> /// <param name="value">图片二进制流</param> private void Cel

Css不规则背景图片在导航里居中显示

在css导航的制作中,出现个问题,以前做的导航都是左右对称的,只要利用margin:0 auto;就可以实现居中.但这次的图片模板中导航的左边宽,右边窄,如果都当成背景来布局,一眼会感觉这个导航没有在中间. 为了让其视觉上logo和导航菜单所在的白色区域居中,就要用相对定位和绝对定位了. left-side.pngright-side.png Html的代码如下: <div id="msg"> <div class="left-side">&

图片在页面中居中显示

参考1:http://www.lanrentuku.com:8081/js/table-188.html# 参考2:http://www.cnblogs.com/xueming/archive/2012/03/21/VerticalAlign.html http://files.cnblogs.com/files/zfanlong1314/%E5%9B%BE%E7%89%87%E5%9C%A8%E9%A1%B5%E9%9D%A2%E4%B8%AD%E5%B1%85%E4%B8%AD%E6%98%

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

我们都知道在ios中,每一个UIImageView都有他的frame大小,但是如果图片的大小和这个frame的大小不符合的时候会怎么样呢?在默认情况,图片会被压缩或者拉伸以填满整个区域. 通过查看UIView的属性可以知道,view的contentMode属性可以用来控制图片的显示情况.下面的设置可以让图片进行居中显示. 1 imageView.contentMode =  UIViewContentModeCenter; 这个居中是包括了,横向和纵向都是居中.图片不会拉伸或者压缩,就是按照im

图片保持比例居中显示

在项目中会遇到这种情况,将图片在一个区域中居中显示,保持图片的比例不变,其他区域用一定的颜色填充.实现的思路就是设置图片的最大宽度为外层div的宽度,图片的最大高度为外层div的高度,这样可以保证图片的比例不变.然后设置居中.具体实现如下. 一 html代码 <div class="photoItem"> <span></span> <a href="#"><img src="8.png"&g

图片大小不改动,根据屏幕大小自动把图片居中显示

background属性 background-image: url('../img/1_1.jpg'); <!-- 背景图片路径 --> background-repeat: no-repeat; <!-- 背景图片是否重复显示 --> background-position: center; <!-- 若背景图片小于div,则居中显示 --> background-attachment: fixed; <!-- 背景图片固定,不随scroll拖动而变动 --&