图片的拉伸

作者: Z了个L | 来源:发表于2016-02-11 22:33 被阅读159次
    • 方式一
        UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
        // 作用: 根据传入需要保护的区域返回一张受保护的图片
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(image.size.height * 0.5, image.size.width * 0.5, image.size.height * 0.5 -1, image.size.width * 0.5 -1);
        UIImage *protectImage = [image resizableImageWithCapInsets:edgeInsets];
        self.imageView.image = protectImage;
    
    • 方式二
        UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(image.size.height * 0.5, image.size.width * 0.5, image.size.height * 0.5 -1, image.size.width * 0.5 -1);
        /*
         UIImageResizingModeTile,  以平铺的方式进行拉伸
         UIImageResizingModeStretch, 直接拉伸平铺
         */
        UIImage *protectImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:UIImageResizingModeTile];
        self.imageView.image = protectImage;
    
    • 方式三
        UIImage *image = [UIImage imageNamed:@"chat_send_nor"];
        // rightCapWidth = width - leftCapWidth - 1
        // bottomCapWidth = height - topCapWidth - 1
        UIImage *protectImage = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height *0.5];
        self.imageView.image = protectImage;
    
    • 方式四
    // 为UIImage添加一个分类
    // UIImage+XMGExtention.h
    #import <UIKit/UIKit.h>
    
    @interface UIImage (XMGExtention)
    /**
     *  返回一张受保护的图片
     *
     *  @param imageName 图片的名称
     *
     *  @return 受保护的图片
     */
    + (instancetype)resizableImageWithImageName:(NSString *)imageName;
    @end
    
    // UIImage+XMGExtention.m
    #import "UIImage+XMGExtention.h"
    
    @implementation UIImage (XMGExtention)
    +(instancetype)resizableImageWithImageName:(NSString *)imageName{
       // 创建图片对象
        UIImage *image = [UIImage imageNamed:imageName];
        // 图片的尺寸
        CGFloat imageWidth = image.size.width * 0.5;
        CGFloat imageHeight = image.size.height * 0.5;
        return [image stretchableImageWithLeftCapWidth:imageWidth topCapHeight:imageHeight];
    }
    @end
    
    
    UIImage *image = [UIImage resizableImageWithImageName:@"chat_send_nor"];
        self.imageView.image = image;
    
    
    • 方式五


    相关文章

      网友评论

        本文标题:图片的拉伸

        本文链接:https://www.haomeiwen.com/subject/tolukttx.html