一、图片的一般格式
bmp是window用的格式 png是无损默认的类型 jpg是压缩图片 gif无法直接加载
二、imageName和imageWithContentOfFile创建图片的区别
imageName创建的是单例,不会被释放,常驻内存,可以加载Assets.xcassets内的图片,可以省略.png后缀
imageView.image = [UIImage imageNamed:@"14"];
NSString * path = [[NSBundle mainBundle] pathForResource:@"14.png" ofType:nil];
imageView.image = [UIImage imageWithContentsOfFile:path];
三、保持原来的样式不失真
[imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
<pre name="code" class="objc">/**
* UIImageResizingModeStretch 拉伸模式
* UIImageResizingModeTile 平铺模式
*/
imageView.image = [imageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20) resizingMode:UIImageResizingModeStretch];
五、图片的翻转
<pre name="code" class="objc">// UIImageOrientationUp, // default orientation 保持原来的状态
// UIImageOrientationDown, // 180 deg rotation 旋转180
// UIImageOrientationLeft, // 90 deg CCW 逆转90
// UIImageOrientationRight, // 90 deg CW 顺转90
// UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
// UIImageOrientationDownMirrored, // horizontal flip
// UIImageOrientationLeftMirrored, // vertical flip
// UIImageOrientationRightMirrored,
imageView.image = [UIImage imageWithCGImage:imageView.image.CGImage scale:1 orientation:UIImageOrientationUp];
六、照片转颜色(照片最好是纯色)
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"homeview_bg"]];
七、颜色转图片
- (UIImage *)imageWithColor:(UIColor*)color
{
CGRect rect=CGRectMake(0,0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
八、改变图片大小(可能会失真,慎用)
-(UIImage *)givePhotoName:(NSString *)name andSize:(CGSize)size{
UIImage * img = [UIImage imageNamed:name];
UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
img= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}