[转] UIImage 图像-IOS开发 (实例)

 

转自  http://justcoding.iteye.com/blog/1470931

一 UIImageView 简介

UIImageView是显示图片的控件,显示图片时,首先需要把图片加载到UIImage控件中,然后根据UIImage控件,设置到UIImageView相对应的属性中

 

1 创建UIImage对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//使用应用程序中图片文件创建,IOS4后可以省略图片扩展名;
+ (UIImage *)imageNamed:(NSString *)name;
 
 
//根据指定的路径创建UIImage
+ (UIImage *)imageWithContentsOfFile:(NSString *)path;
- (instancetype)initWithContentsOfFile:(NSString *)path;
 
 
//使用NSData创建
+ (UIImage *)imageWithData:(NSData *)data;
- (instancetype)initWithData:(NSData *)data;
 
 
//使用Quartz 2D对象创建UIImage
+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage;
- (instancetype)initWithCGImage:(CGImageRef)cgImage;

 

注意:

UIImage imageNamed 会自动缓存图标,会占用大量内存,推荐使用 IImage imageWithContentOfFile:(NSString *);

 

2设置UIImage对象到UIImageView中
1
2
3
4
5
6
//单个图片对象
@property(nonatomic,retain) UIImage *image;
 
 
//用数组存储的多个图片对象
@property(nonatomic,copy) NSArray *animationImages;

 

3 创建UIImageView
1
2
3
4
5
6
7
//创建image对象
UIImage *image = [UIImage imageNamed:@"imageName"];
UIImage *image = [UIImage imageWithContentsOfFile:[bundle pathForResource:@"imageName" ofType:nil]]
 
 
//添加image对象到UIImageView中
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];

 

二 UIImageView 动画功能

查看UIImageView头文件的相关属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//设置图片,animationImages 是NSArray数组中包含的UIImage
@property (nonatomic,copy) NSArray *animationImages;
 
//设置播放次数,不指定为无限循环播放
@property (nonatomic)NSInteger animationRepeatCount;
 
//设置播放速度,动画持续的时间
@property (nonatomic)animationDuration;
 
//启动动画
- (void)startAnimating;
 
//停止动画
- (void)stopAnimating;
 
//判断动画是否在运行
- (BOOL)isAnimating;

 

注意

在动画执行完毕后,需要清理缓存,把UIImageView赋值为nil

 

三 汤姆猫代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@interface ViewController ()
 
/**
 *  属性
 */
@property (weak, nonatomic) IBOutlet UIImageView *tomImageView;
 
 
/**
 *  喝牛奶
 */
-(IBAction)drink;
-(IBAction)cymbal;
-(IBAction)fart;
-(IBAction)pie;
-(IBAction)scratch;
-(IBAction)eat;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
 
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
 
/**
 *  喝牛奶
 */
-(IBAction)drink
{
    [self runAnimationImagesWithFilePrefix: @"drink" andFileCount:80];
}
 
-(IBAction)cymbal
{
    [self runAnimationImagesWithFilePrefix: @"cymbal" andFileCount:12];
}
 
-(IBAction)fart
{
    [self runAnimationImagesWithFilePrefix: @"fart" andFileCount:27];
}
 
-(IBAction)pie
{
    [self runAnimationImagesWithFilePrefix: @"pie" andFileCount:23];
}
 
-(IBAction)scratch
{
    [self runAnimationImagesWithFilePrefix: @"scratch" andFileCount:55];
}
 
-(IBAction)eat
{
    [self runAnimationImagesWithFilePrefix: @"eat" andFileCount:39];
}
 
 
 
 
 
/**
 *  播放动画
 *
 *  @param filePrefix 动画图片前缀
 *  @param count      动画图片张数
 */
 
-(void)runAnimationImagesWithFilePrefix:(NSString*)filePrefix andFileCount:(NSInteger)count
{
     
    NSInteger imageTotal = count;
    //    1.在动画播放时,不能点击
    if([self.tomImageView isAnimating]){
        return;
    }
     
    //    2.保存图片到数组
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:imageTotal];
    for (int i=0; i<=imageTotal; i++) {
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",filePrefix,i];
        [images addObject:[UIImage imageWithContentsOfFile:[bundle pathForResource:imageName ofType:nil]]];
    }
     
    //    3.把数组赋值给tomImageView
    self.tomImageView.animationImages=images;
     
     
    //    4.播放动画
    NSInteger delay = images.count*0.09;
    //    4.1设置播放次数
    self.tomImageView.animationRepeatCount=1;
    //    4.2设置播放持续时间
    self.tomImageView.animationDuration=delay;
    //    4.3开始播放
    [self.tomImageView startAnimating];
     
    //    5调用清空缓存
    [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
}
 
 
/**
 *  清空缓存
 */
-(void)clearCache
{
    self.tomImageView.animationImages=nil;
}
@end

UIKit中有一些类可以用来操纵单个图像,还有一个图像类可以用来显示图像。Apple还提供了一种特殊的导航控制器,用于从图像库中选择图像。

 

UIImage类对图像及其底层数据进行封装。它可以直接绘制在一个视图内,或者作为一个图像容器在另一个更大的图像视图容器中使用。这个类类提供 的方法可以用来从各种来源中载入图像,在屏幕上设置图片的方向,以及提供有关图像的信息。对于简单的图形应用,可以将UIImage对象用在视图类的 drawRect方法中,用来绘制图像和团模板。

 

你可以用文件来初始化,也可以用url、原始数据、或者一个Core Graphics图像的内容。静态方法(类方法)和实例方法都有;这些方法可以引用并缓存已有的图像内容,也可以实例化新的图像对象,如何使用完全取决于应用程序的需要。

 

使用一个图像的最简单方法就是通过静态方法。静态方法不会去管理图像的实例,与之相反,他们提供了直接的接口,可以用来共享位于框架内部的记忆体缓存对象。这有助于保持应用程序的整洁,也会生去做清理工作的需要。静态方法和实例方法都可以用来创建相同的对象。

 

一、使用文件创建(静态方法)

C代码  收藏代码
  1. UIImage *myImage = [UIImage imageNamed:@"ppp"];   

 

二、使用 URL 和原始数据(静态方法)

C代码  收藏代码
  1. NSData *imageData = [ NSData initWithBytes:image:imagePtr length:imageSize ]; // 假设 imagePtr 是一个指向原始数据的指针    
  2. UIImage* myImage = [ [ UIImage alloc ]initWithData:imageData ];    

 

C代码  收藏代码
  1. UIImage *myImage2 =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]]];   

 

三、使用Core Graphics (静态方法)   

//这里可能是实例方法,以后我会这个UIImage这个知识点整理一下。

C代码  收藏代码
  1. UIImage* myImage3 = [UIImage imageWithCGImage:myCGImageRef];   

 

四、使用文件(实例方法)

C代码  收藏代码
  1. UIImage* myImage4 = [[UIImage alloc]initWithContentsOfFile:[NSString stringWithFormat:@"%@/Documents/ppp.png",NSHomeDirectory()]];    

 

五、使用 URL 和原始数据(实例方法)

如果图像存储在内存中,你可以创建一个NSData 对象作为initWithData 方法的原始输入,来初始化一个UIImage对象。

 

如果图像是一张网络图片,可以使用NSData来进行预载,然后用它来初始化UIImage对象:

C代码  收藏代码
  1. UIImage *myImage5 =[ [ UIImage alloc]initWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.kutx.cn/xiaotupian/icons/png/200803/20080327095245737.png"]] ];   

 

六、使用Core Graphics (实例方法)

C代码  收藏代码
  1. UIImage* myImage6 = [[UIImage alloc]initWithCGImage:myCGImageRef];    

 

七、显示图像

当视图类的drawRect 方法被唤起时,它们会调用内部的回吐例程。与其他图像类不同,UIImage对象不能被当成子 ,直接附着在其他视图上,因为他不是一个视图类。反过来,一个UIView类则可以在视图的drawRect例程中,调用图像的 drawRect 方法。这可以使得图像显在UIView类的显示区域内部。

 

只要一个视图对象的窗口的某些部分需要绘制,就可以调用它的drawRect方法。要在窗口内 部显示一个 UIImage 的内容,可以调用该对象的 drawRect 方法:

C代码  收藏代码
  1. - (void)drawRect:(CGRect)rect{    
  2.     CGRect myRect;    
  3.         
  4.     myRect.origin.x = 0.0 ;    
  5.     myRect.origin.y = 0.0;    
  6.     myRect.size = myImage.size;    
  7.     [myImage drawInRect:myRect];    
  8. }    

 

注意不要在drawRect方法内分配任何新对象,因为他在每次窗口重绘时都被调用。

只有在视图初次绘制时,才会调用drawRect方法。要强制更新,可以使用视图类的 setNeedsDisplay 或者 setNeedsDisplayInRect  方法:

C代码  收藏代码
  1. [myView setNeedsDisplay];    
  2.     [myView setNeedsDisplayInRect:self.view];    

 

八、绘制图案

如果图像是一个图案模板,你可以用UIImage类提供的另外一个方法 drawAsPatternInrect,在整个视图区域重复绘制该图像:

C代码  收藏代码
  1. UIView* myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];    
  2.     [myImage drawInRect:myView.frame];    
  3.     [self.view addSubview:myView];  

 

九、方向

一个图像的方向,决定了它在屏幕上如何被旋转。因为iPhone 能被以6种不同的方式握持,所以在方向改变时,能够将图像做相应的旋转就十分必要了。UIImage 有个只读属性 imageOrientation 来标识它的方向。

C代码  收藏代码
  1. UIImageOrientation myOrientation =  myImage.imageOrientation ;    

 

可以设置以下方向:

C代码  收藏代码
  1. typedef enum {    
  2.     UIImageOrientationUp,            // default orientation  默认方向    
  3.     UIImageOrientationDown,          // 180 deg rotation    旋转180度    
  4.     UIImageOrientationLeft,          // 90 deg CCW         逆时针旋转90度    
  5.     UIImageOrientationRight,         // 90 deg CW          顺时针旋转90度    
  6.     UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip   向上水平翻转    
  7.     UIImageOrientationDownMirrored,  // horizontal flip    向下水平翻转    
  8.     UIImageOrientationLeftMirrored,  // vertical flip      逆时针旋转90度,垂直翻转    
  9.     UIImageOrientationRightMirrored, // vertical flip      顺时针旋转90度,垂直翻转    
  10. } UIImageOrientation;    

 

十、图像尺寸

你可以通过size属性读取一个图像的尺寸,得到一个CGSize 结构,其中包含 wifth 和height 。

C代码  收藏代码
  1. CGSize myImageSize = myImage.size;    

 

 

十一、UIImageview 点击事件

 

C代码  收藏代码
  1. UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];  
  2. imageView.image=[UIImageimageNamed:@"filter_laozhaopian_a.png"];  
  3.    
  4. imageView.userInteractionEnabled = YES;  
  5. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(UesrClicked:)];  
  6. [imageView addGestureRecognizer:singleTap];  
  7. [singleTap release];  
  8.       
  9. [self.view addSubview:imageView];  

 

 

来源:http://blog.csdn.net/iukey/article/details/7308433

 

 

 

使用UIImageView来播放动画.

 

C代码  收藏代码
  1. -(void)viewDidLoad  
  2. {  
  3.     [superviewDidLoad];  
  4.   
  5.     self.title=NSLocalizedString(@"ImagesTitle",@"");  
  6.     //setupourUIImagewithagrouporarrayofimagestoanimate(orinourcaseaslideshow)    
  7.     self.imageView.animationImages = [NSArrayarrayWithObjects:[UIImageimageNamed:@"scene1.jpg"],[UIImageimageNamed:@"scene2.jpg"],[UIImageimageNamed:@"scene3.jpg"],[UIImageimageNamed:@"scene4.jpg"],[UIImageimageNamed:@"scene5.jpg"],  nil  ];  
  8.   
  9.     imageView.animationDuration=5.0;  
  10.     [self.imageViewstopAnimating];  
  11.   
  12.     //Settheappropriateaccessibilitylabels.  
  13.     [self.imageViewsetIsAccessibilityElement:YES];  
  14.     [self.imageViewsetAccessibilityLabel:self.title];  
  15.     [self.slidersetAccessibilityLabel:NSLocalizedString(@"DurationSlider",@"")];  
 
 
 
posted on 2015-05-27 14:19  MichaelMao  阅读(562)  评论(0编辑  收藏  举报