图片的拉伸、翻转、不变形,图片转颜色,本地加载

一、图片的一般格式

bmp是window用的格式    png是无损默认的类型    jpg是压缩图片  gif无法直接加载

二、imageName和imageWithContentOfFile创建图片的区别

imageName创建的是单例,不会被释放,常驻内存,可以加载Assets.xcassets内的图片,可以省略.png后缀

imageView.image = [UIImage imageNamed:@"14"];


imageWithContentOfFile 加载的图片最终会被释放,但不能加载Assets.xcassets内的图片,图片的后缀不能省略

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;
}


备注:欢迎指正,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值