iOS动画详解(二)

UIImage常用的绘图操作
 
一个UIImage对象提供了向当前上下文绘制自身的方法。我们现在已经知道如何获取一个图片类型的上下文并将它转变成当前上下文。
 
平移操作:下面的代码展示了如何将UIImage绘制在当前的上下文中。
 1 - UIImage* mars = [UIImage imageNamed:@"Mars.png"];
 2 -
 3 - CGSize sz = [mars size];
 4 -
 5 - UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width*2, sz.height), NO, 0);
 6 -
 7 - [mars drawAtPoint:CGPointMake(0,0)];
 8 -
 9 - [mars drawAtPoint:CGPointMake(sz.width,0)];
10 -
11 - UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
12 -
13 - UIGraphicsEndImageContext();
14 -
15 - UIImageView* iv = [[UIImageView alloc] initWithImage:im];
16 -
17 - [self.window.rootViewController.view addSubview: iv];
18 -
19 -     iv.center = self.window.center;
                                   图1 UIImage平移处理
 
 缩放操作:下面代码展示了如何对UIImage进行缩放操作:
 1 - UIImage* mars = [UIImage imageNamed:@"Mars.png"];
 2 -
 3 - CGSize sz = [mars size];
 4 -
 5 - UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width*2, sz.height*2), NO, 0);
 6 -
 7 - [mars drawInRect:CGRectMake(0,0,sz.width*2,sz.height*2)];
 8 -
 9 - [mars drawInRect:CGRectMake(sz.width/2.0, sz.height/2.0, sz.width, sz.height) blendMode:kCGBlendModeMultiply alpha:1.0];
10 -
11 - UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
12 -
13 - UIGraphicsEndImageContext();
                              图2 UIImage缩放处理
以上的代码首先创建一个一半图片宽度的图形上下文,然后将图片左上角原点移动到与图形上下文负X坐标对齐,从而让图片只有右半部分与图形上下文相交。
             
 
                             图3 UIImage裁剪原理
 
 
posted @ 2016-06-13 16:56  抠得儿  阅读(182)  评论(0)    收藏  举报