美文网首页iOS开发知识小集
iOS 图片拉伸:两边拉伸,中间保持不变

iOS 图片拉伸:两边拉伸,中间保持不变

作者: _huawuque | 来源:发表于2017-04-03 11:42 被阅读615次
    WX20170403-114055.png

    (网络图片,侵删)
    创建UIImage分类中的方法
    - (UIImage *)dc_stretchLeftAndRightWithContainerSize:(CGSize)imageViewSize
    {
    CGSize imageSize = self.size;
    CGSize bgSize = CGSizeMake(floorf(imageViewSize.width), floorf(imageViewSize.height)); //imageView的宽高取整,否则会出现横竖两条缝

    UIImage *image = [self stretchableImageWithLeftCapWidth:imageSize.width *0.8 topCapHeight:imageSize.height * 0.5];
    CGFloat tempWidth = (bgSize.width)/2 + (imageSize.width)/2;
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(tempWidth, bgSize.height), NO, [UIScreen mainScreen].scale);
    
    [image drawInRect:CGRectMake(0, 0, tempWidth, bgSize.height)];
    
    UIImage *firstStrechImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImage *secondStrechImage = [firstStrechImage stretchableImageWithLeftCapWidth:imageSize.width *0.2 topCapHeight:imageSize.height*0.5];
    
    return secondStrechImage;
    }

    相关文章

      网友评论

        本文标题:iOS 图片拉伸:两边拉伸,中间保持不变

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