iOS中图片的拉伸处理

Snip20160320_2.png
](http://upload-images.jianshu.io/upload_images/1455379-13f09a4cf44c81bb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

开发中可能用法.png
  • 很明显,开发给我们的图不能满足我们开发的需求,这时候我们就要对图片进行拉伸

    • 1、
    UIImageView *img = [[UIImageView alloc] init];
    img.frame = CGRectMake(20, 100, 300, 300);
    [self.view addSubview:img];
    
    UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
    img.image = image;
   
    // 第一种代码处理方法1
    // resizableImageWithCapInsets保护区域,通过设置这个值来达到我们想要的效果
    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(30, 30, 30, 30) resizingMode:UIImageResizingModeStretch];
    // 后面的保护模式有两种:
//    UIImageResizingModeStretch 拉伸模式
//    UIImageResizingModeTile 默认是平铺模式
  • 2、
    UIImageView *img = [[UIImageView alloc] init];
    img.frame = CGRectMake(20, 100, 300, 300);
    [self.view addSubview:img];
    
    UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
    img.image = image;
    
    // 还有一种方法2,很久以前的方法
    // 我们给一个左边的值和一个上边的值,苹果自动帮我们计算出下面和右边的值,并对中间1*1的区域进行拉伸
    image = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2];
  • 3、
    通过Assets.xcassets来设置
1.png

![2.png]](http://upload-images.jianshu.io/upload_images/1455379-abd0b357bf18f881.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容