美文网首页GXiOS
iOS开发--UIImage图片拉伸

iOS开发--UIImage图片拉伸

作者: Caesar_62dd | 来源:发表于2019-05-06 22:13 被阅读3次

方法一、新建一个分类(Category),命名:UIImage+GXExtention

UIImage+GXExtention.h文件

#import <UIKit/UIKit.h>
@interface UIImage (GXExtention)
/**
 * 返回一张受保护的图片(被拉伸的)
 */
+ (instancetype)resizableImageWithLocalImageName: (NSString *)localImageName;
@end

UIImage+GXExtention.m文件

#import "UIImage+GXExtention.h"
@implementation UIImage (GXExtention)
+ (instancetype)resizableImageWithLocalImageName:(NSString *)localImageName{
   // 创建图片对象
    UIImage *image = [UIImage imageNamed:localImageName];
    
    // 获取图片的尺寸
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeiht = image.size.height;
    
    // 返回一张拉伸且受保护的图片
    return [image stretchableImageWithLeftCapWidth:imageWidth * 0.5 topCapHeight:imageHeiht * 0.5 ];
}
@end

方法二、在Assets.xcassets中设置,从源头上拉伸图片.避免每次调用方法.

屏幕快照 2019-05-06 22.04.28.png

相关文章

网友评论

    本文标题:iOS开发--UIImage图片拉伸

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