图像控件 (UIImageView)


图像控件 (UIImageView)

1. UIImageView 控件属性

(1) UIImageView 简介

UIImageView 简介 

-- 继承结构 : UIImageView 继承 UIView, 该类不能响应用户操作, 是静态控件, (活动控件 静态控件 被动控件);

 

(2) 图片显示属性

 

设置图片显示的属性 : 

-- image (普通) : 访问或设置该控件显示的图片;

-- HighlightedImage (高亮) : 设置图片处于 高亮状态 时显示的图片;

(3) 动画显示方法

 

UIImageView 动画显示方法 : 

-- animationImages : 设置一个 NSArray 对象, 需要显示多张图片;

-- highlightedAnimationImages : 设置 高亮状态 显示的多张图片;

-- animationDuration : 设置 UIImageView 动画持续时间;

-- animationRepeatCount : 设置 UIImageView 动画重复次数;

-- startAnimating : 开始播放动画;

-- stopAnimating : 停止播放动画;

-- isAnimating : 判断 UIImageView 是否正在播放动画;

 

(4) UIImageView 缩放属性

 

UIImageView 缩放属性 : 

     

-- Scale To Fill : 不保持 纵横缩放比, 图片完全自适应 UIImageView 控件;

-- Aspect Fit : 保持纵横比缩放, 保证图片长边完全显示出来, 完整显示图片;

-- Aspect Fill : 保持纵横比缩放, 保证图片短边能显示出来, 只在水平或垂直方向某一个方向是完整的, 另一个方向截取;

-- Center : 不缩放图片, 显示图片的中间区域;

-- Top : 不缩放图片, 显示图片的顶部区域;

-- Bottom : 不缩放图片, 显示图片底部区域;

-- Left : 不缩放图片, 显示图片左边区域;

-- Right : 不缩放图片, 显示图片右边区域;

-- Top Left : 不缩放图片, 显示图片左上区域;

-- Top Right : 不缩放图片, 显示图片右上区域;

-- Bottom Left : 不缩放图片, 显示图片左下区域;

-- Bottom Right : 不缩放图片, 显示图片右下区域;

2. 图片浏览器示例

(1) API 简介

手势事件 : 

-- 设置手势点击响应 : 每个 UIView 都有一个 userInteractionEnabled 属性为 YES;

 

[objc] view plaincopy
 
  1. //设置大图片可以相应手势点击  
  2. self.bigPicture.userInteractionEnabled = YES;  



 

-- 创建手势识别器 : 创建 UITapGestureRecognizer 手势识别器, initWithTarget 表示手势响应方法的类, action 对应方法的 selector 方法;

 

[objc] view plaincopy
 
  1. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];  


-- 为 UIView 添加手势识别器 : 调用 UIView 的 addGestureRecognizer 方法;

 

[objc] view plaincopy
 
  1. [self.bigPicture addGestureRecognizer:tap];  


-- 设置 UIImageView 图片 : 

 

[objc] view plaincopy
 
  1. self.bigPicture.image = [UIImage imageNamed:[images objectAtIndex:currentImage%images.count]];  


-- 设置透明度 : 

 

[objc] view plaincopy
 
  1. self.bigPicture.alpha = alpha;  


-- 获取手指触摸位置 : 

 

[objc] view plaincopy
 
  1. //获取手指触摸的位置  
  2. CGPoint point = [recognizer locationInView:self.bigPicture];  


-- 获取图片对应的 CGImageRef : 

 

[objc] view plaincopy
 
  1. //获取原图对应的 CGImageRef  
  2. CGImageRef imageRef = [srcImage CGImage];  


-- 根据一个图片创建新的 CGImageRef : 

 

[objc] view plaincopy
 
  1. //创建新的图片  
  2. CGImageRef newImageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(x, y, 140, 140));  


-- 根据 CGImageRef 创建 UIImage : 

 

[objc] view plaincopy
 
  1. [UIImage imageWithCGImage:newImageRef];   

(2) 代码示例

代码示例 : 

-- 界面设计文件 : 

-- OCViewController.h : 

 

[objc] view plaincopy
 
  1. //  
  2. //  OCViewController.h  
  3. //  UIImageView  
  4. //  
  5. //  Created by octopus on 15-12-7.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface OCViewController : UIViewController  
  12.   
  13. //大图片的 UIImageView  
  14. @property (strong, nonatomic) IBOutlet UIImageView *bigPicture;  
  15. //小图片的 UIImageView  
  16. @property (strong, nonatomic) IBOutlet UIImageView *smallPicture;  
  17.   
  18. //UISegmentedControl 的方法  
  19. - (IBAction)segmentControl:(id)sender;  
  20.   
  21. @end  



 

-- OCViewController.m : 

 

[objc] view plaincopy
 
  1. //  
  2. //  OCViewController.m  
  3. //  UIImageView  
  4. //  
  5. //  Created by octopus on 15-12-7.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import "OCViewController.h"  
  10.   
  11. @interface OCViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation OCViewController  
  16.   
  17. //图片集合  
  18. NSArray * images;  
  19. //当前显示的图片  
  20. int currentImage;  
  21. //透明度  
  22. CGFloat alpha;  
  23.   
  24. - (void)viewDidLoad  
  25. {  
  26.     [super viewDidLoad];  
  27.     // Do any additional setup after loading the view, typically from a nib.  
  28.       
  29.     //初始化变量  
  30.     currentImage = 0;  
  31.     alpha = 1.0;  
  32.     images = [NSArray arrayWithObjects:@"1.png" , @"2.jpg", @"3.png", nil nil];  
  33.       
  34.     //设置大图片可以相应手势点击  
  35.     self.bigPicture.userInteractionEnabled = YES;  
  36.       
  37.     UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];  
  38.       
  39.     [self.bigPicture addGestureRecognizer:tap];  
  40.       
  41. }  
  42.   
  43. - (void)didReceiveMemoryWarning  
  44. {  
  45.     [super didReceiveMemoryWarning];  
  46.     // Dispose of any resources that can be recreated.  
  47. }  
  48.   
  49. - (IBAction)segmentControl:(id)sender {  
  50.     int index = [sender selectedSegmentIndex];  
  51.     switch (index) {  
  52.         case 0: //透明度+  
  53.             alpha += 0.1;  
  54.             if(alpha > 1.0){  
  55.                 alpha = 1.0;  
  56.             }  
  57.             self.bigPicture.alpha = alpha;  
  58.             break;  
  59.         case 1: //透明度-  
  60.             NSLog(@"1");  
  61.             alpha -= 0.1;  
  62.             if(alpha < 0){  
  63.                 alpha = 0;  
  64.             }  
  65.             self.bigPicture.alpha = alpha;  
  66.             break;  
  67.         case 2: //下一张图片  
  68.             NSLog(@"2");  
  69.             self.bigPicture.image = [UIImage imageNamed:[images objectAtIndex:currentImage%images.count]];  
  70.             currentImage ++;  
  71.             break;  
  72.               
  73.         default:  
  74.             break;  
  75.     }  
  76. }  
  77.   
  78. - (void) click : (UIGestureRecognizer * ) recognizer{  
  79.     //获取正在显示的图片  
  80.     UIImage * srcImage = self.bigPicture.image;  
  81.     //获取手指触摸的位置  
  82.     CGPoint point = [recognizer locationInView:self.bigPicture];  
  83.     //获取原图对应的 CGImageRef  
  84.     CGImageRef imageRef = [srcImage CGImage];  
  85.     //获取缩放比例  
  86.     CGFloat scale = srcImage.size.width / 320;  
  87.     //获取图片位置  
  88.     CGFloat x = point.x * scale;  
  89.     CGFloat y = point.y * scale;  
  90.     //验证 x y 坐标, 不要超出边界  
  91.     if (x + 120 > srcImage.size.width - 140) {  
  92.         x = srcImage.size.width - 140;  
  93.     }  
  94.       
  95.     if (y + 120 > srcImage.size.height) {  
  96.         y = srcImage.size.height - 140;  
  97.     }  
  98.       
  99.     //创建新的图片  
  100.     CGImageRef newImageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(x, y, 140, 140));  
  101.     self.smallPicture.image = [UIImage imageWithCGImage:newImageRef];  
  102.       
  103. }  
  104. @end  



 

-- 界面展示 : 

 

3. 幻灯片放映

(1) API 简介

 

幻灯片播放相关 API : 

-- 设置 UIImage 数组给 UIImageView : 

 

[objc] view plaincopy
 
  1. images = [NSArray arrayWithObjects:  
  2.           [UIImage imageNamed:@"1.png"],  
  3.           [UIImage imageNamed:@"2.jpg"],  
  4.           [UIImage imageNamed:@"3.png"],  
  5.           nil nil];  
  6. self.imageView.animationImages = images;  


-- 设置动画的间隔 和 次数 : 

 

 

[objc] view plaincopy
 
  1. //设置 UIImageView 动画间隔  
  2. self.imageView.animationDuration = 5;  
  3. //设置动画重复次数  
  4. self.imageView.animationRepeatCount = 0xFFFF;  


-- 启动动画 : 

 

[objc] view plaincopy
 
  1. //启动动画  
  2. [self.imageView startAnimating];  

(2) 代码示例

代码示例 : 

-- 界面设计文件 : 

-- OCViewController.h : 

 

[objc] view plaincopy
 
  1. //  
  2. //  OCViewController.h  
  3. //  UIimageView2  
  4. //  
  5. //  Created by octopus on 15-12-9.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface OCViewController : UIViewController  
  12. @property (strong, nonatomic) IBOutlet UIImageView *imageView;  
  13.   
  14. @end  

 

-- OCViewController.m : 

 [objc] view plaincopy

 
  1. //  
  2. //  OCViewController.m  
  3. //  UIimageView2  
  4. //  
  5. //  Created by octopus on 15-12-9.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import "OCViewController.h"  
  10.   
  11. @interface OCViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation OCViewController  
  16.   
  17. NSArray * images;  
  18.   
  19. - (void)viewDidLoad  
  20. {  
  21.     [super viewDidLoad];  
  22.     // Do any additional setup after loading the view, typically from a nib.  
  23.       
  24.     //创建 UIImageView 集合  
  25.     images = [NSArray arrayWithObjects:  
  26.               [UIImage imageNamed:@"1.png"],  
  27.               [UIImage imageNamed:@"2.jpg"],  
  28.               [UIImage imageNamed:@"3.png"],  
  29.               nil nil];  
  30.     //设置集合给 UIImageView 的动画  
  31.     self.imageView.animationImages = images;  
  32.     //设置 UIImageView 动画间隔  
  33.     self.imageView.animationDuration = 5;  
  34.     //设置动画重复次数  
  35.     self.imageView.animationRepeatCount = 0xFFFF;  
  36.     //启动动画  
  37.     [self.imageView startAnimating];  
  38.       
  39. }  
  40.   
  41. - (void)didReceiveMemoryWarning  
  42. {  
  43.     [super didReceiveMemoryWarning];  
  44.     // Dispose of any resources that can be recreated.  
  45. }  
  46.   
  47. @end  



 

-- 界面展示 : 

 

posted @ 2016-01-07 09:56  sungk  阅读(447)  评论(0编辑  收藏  举报