UIImage相关
设置尺寸
imageView.frame = CGRectMake(0, 0, 100, 100);
设置图片资源
- 方式一
/**
加载Assets.xcassets这里面的图片:
1> 打包后变成Assets.car
2> 拿不到路径
3> 只能通过imageNamed:来加载图片
4> 不能通过imageWithContentsOfFile:来加载图片
*/
imageView.image = [UIImage imageNamed:@"1"];
- 方式二
/**
放到项目中的图片:
1> 可以拿到路径
2> 能通过imageNamed:来加载图片
3> 也能通过imageWithContentsOfFile:来加载图片
*/
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
imageView.image = [UIImage imageWithContentsOfFile:path];
设置图片的内容模式
/**
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
*/
imageView.contentMode = UIViewContentModeScaleAspectFill;
裁剪多余的部分
imageView.clipsToBounds = YES;
设置frame的方式
- 方式一
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"1"];
imageView.frame = CGRectMake(100, 100, 267, 400);
imageView.frame = (CGRect){{100, 100},{267, 400}};
- 方式二
UIImageView *imageView = [[UIImageView alloc] init];
// 创建一个UIImage对象
UIImage *image = [UIImage imageNamed:@"1"];
// 设置frame
imageView.frame = CGRectMake(100, 10, image.size.width, image.size.height);
// 设置图片
imageView.image = image;
- 方式三
UIImage *image = [UIImage imageNamed:@"1"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 10, image.size.width, image.size.height)];
imageView.image = image;
- 方式四
// 创建一个UIimageview对象
// 注意: initWithImage 默认就有尺寸--->图片的尺寸
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
创建帧动画
NSMutableArray<UIImage *> *imageArr = [NSMutableArray array];
for (int i=0; i<20; i++) {
// 获取图片的名称
NSString *imageName = [NSString stringWithFormat:@"%d", i+1];
// 创建UIImage对象
UIImage *image = [UIImage imageNamed:imageName];
// 加入数组
[imageArr addObject:image];
}
// 设置动画图片
imageView.animationImages = imageArr;
设置动画的播放次数
imageView.animationRepeatCount = 0;
设置播放时长
// 1秒30帧, 一张图片的时间 = 1/30 = 0.03333 20 * 0.0333
imageView.animationDuration = 1.0;
开始动画
[self.imageView startAnimating];
结束动画
[self.imageView stopAnimating];
九宫格缩放图片(用于类似微信,短信的气泡框)
//创建UIImage对象
UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
//创建UIImage对象
CGFloat imageWidth = image.size.width;
CGFloat imageHeight = image.size.height;
// 方式一
UIImage *resizableImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageHeight * 0.5, imageWidth * 0.5, imageHeight * 0.5 -1, imageWidth * 0.5 - 1)];
//UIImageResizingModeTile, 平铺
//UIImageResizingModeStretch, 拉伸(伸缩)
UIImage *resizableImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageHeight * 0.5, imageWidth * 0.5, imageHeight * 0.5 -1, imageWidth * 0.5 - 1) resizingMode:UIImageResizingModeTile];
// 方式二
// 右边需要保护的区域 = 图片的width - leftCapWidth - 1
UIImage *resizableImage = [image stretchableImageWithLeftCapWidth:imageWidth * 0.5 topCapHeight:imageHeight * 0.5];