- 1、設(shè)置動畫
// 1)添加imageView
UIImageView *imageView = [[UIImageView alloc]
initWithFrame:self.bounds];
// 2)動畫照片數(shù)組
NSMutableArray*arrM = [NSMutableArrayarray];
for (inti = 1; i < 9;i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
[arrM addObject:image];
}
// 3)給照片數(shù)組賦給imageView的動畫數(shù)組
imageView.animationImages = arrM;
// 4)設(shè)置動畫播放屬性
imageView.animationRepeatCount =1;
imageView.animationDuration =0.5;
// 5)開始動畫
[imageView startAnimating];
[self addSubview:imageView];
-
2、將圖片按原始尺寸添加到ImageView上
1)storyboard或xib中實現(xiàn):
在imageView的Mode屬性中設(shè)置center;-
2)在圖片中實現(xiàn):
1】
Snip20150827_4.png
2】
Snip20150827_5.png
3】在render As中選擇Original Image即可
Snip20150827_7.png 3)代碼實現(xiàn):
// 調(diào)用UIImage的對象方法---設(shè)置UIImageRenderingMode參數(shù)為:UIImageRenderingModeAlwaysOriginal - (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode; // 示例:如果一個項目中多處用到以上方法:即將圖片按原尺寸添加,可以給UIImage添加一個分類: // 1】.h文件 @interface UIImage (Image) // 快速的返回一個最原始的圖片 + (instancetype)imageWithOriRenderingImage:(NSString *)imageName; @end // 2】.m文件 #import <UIKit/UIKit.h> #import "UIImage+Image.h" @implementation UIImage (Image) + (instancetype)imageWithOriRenderingImage:(NSString *)imageName { UIImage *image = [UIImage imageNamed:imageName]; // UIImageRenderingModeAlwaysOriginal--保持原始圖片模式 return[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } @end
- 3、將小圖片拉伸填充ImageView---并保證拉伸后的像素與之前一樣
- 1)圖片中實現(xiàn):
1】

2】

3】在Slices中選擇Horizontal and Vertical即可

- 2)代碼實現(xiàn):
```objc
// 調(diào)用UIImage的對象方法---設(shè)置UIImageRenderingMode參數(shù)為:UIImageRenderingModeAlwaysOriginal
- (UIImage *)stretchableImageWithLeftCapWidth: topCapHeight: ;
// 示例:如果一個項目中多處用到以上方法:即將圖片圖片拉伸添加,可以給UIImage添加一個分類:
// .h 文件
#import <UIKit/UIKit.h>
@interface UIImage (ICKImage)
+ (UIImage *)imageWithStretchableImageName:(NSString *)imageName;
@end
// .m 文件
+ (UIImage *)imageWithStretchableImageName:(NSString *)imageName
{
UIImage *image = [UIImage imageNamed:imageName];
/*
* LeftCapWidth:圖片水平拉伸的點,設(shè)為0.5,即是從水平中點拉伸
* topCapHeight:圖片豎直拉伸的點,設(shè)為0.5,即是從豎直中點拉伸
*/
return[image stretchableImageWithLeftCapWidth:0.5topCapHeight:0.5];
}
- 4、將矩形圖片截成圓角圖片:圖層方法和繪畫方法
- 利用圖層實現(xiàn):
// 設(shè)置頭像圓角半徑
self.titleImage.layer.cornerRadius =self.titleImage.frame.size.width*0.5;
//切除圓角以外部分
self.titleImage.layer.masksToBounds= YES;
//設(shè)置圓角邊緣寬度(如果不需要邊緣,可省略)
self.titleImage.layer.borderWidth= 8;
//設(shè)置邊框顏色
self.titleImage.layer.borderColor= [UIColorcolorWithRed:36/225.0green:36/225.0blue:36/225.0alpha:1].CGColor;
- 2)利用繪畫實現(xiàn)
// 給image添加一個圓角圖片方法
-(UIImage*)circleImage
{
// 開啟圖片上下文
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0);
// 獲取圖片上下文
CGContextRefctx = UIGraphicsGetCurrentContext();
// 給上下文添加一個圓
CGRect circle =CGRectMake(0,0, self.size.width,self.size.height);
CGContextAddEllipseInRect(ctx, circle);
// 剪切圖片上下文
CGContextClip(ctx);
// 將圖片畫至剪切的上下文
[self drawInRect:circle];
// 從上下文獲取圖片
UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
// 關(guān)閉上下文
UIGraphicsEndImageContext();
return image;
}
特別提醒:
雖然圖層比繪畫實現(xiàn)起來簡單,但是通過圖層設(shè)置時,系統(tǒng)內(nèi)部會進行一系列的底層操作,從而會導致內(nèi)部運行不流暢,尤其是多個控件的圖片通過圖層設(shè)置成圓角圖片。
建議:
1、涉及較少量的圖片進行圓角圖片設(shè)置時,可運用圖層設(shè)置;
2、如果是很多圖片進行圓角圖片設(shè)置時,最好使用繪畫,不會太消耗內(nèi)部資源。


