美文网首页
ios中图片拉伸的几种方式

ios中图片拉伸的几种方式

作者: seventhboy | 来源:发表于2016-11-23 17:07 被阅读25次
    1. UIImageView整体拉伸

    UIImageView-contentMode

    typedef NS_ENUM(NSInteger, UIViewContentMode) {
    UIViewContentModeScaleToFill, // 默认 拉伸(会变形)
    UIViewContentModeScaleAspectFit, // 等比例拉伸
    UIViewContentModeScaleAspectFill, // 等比例填充
    UIViewContentModeRedraw, // redraw on bounds change (这个不清楚)
    UIViewContentModeCenter, // 下面的就是不拉伸按位置显示了
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,
    };

    1. UIImage局部拉伸

    // 按4边间距显示不拉伸的区域

    • (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0);
      // 按2点拉伸
    • (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
    • (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
      // 拉伸模式
      typedef NS_ENUM(NSInteger, UIImageResizingMode) {
      UIImageResizingModeTile,//进行区域复制模式拉伸
      UIImageResizingModeStretch,//进行渐变复制模式拉伸
      };

    3.UIImage修改大小

    //内缩放,一条变等于最长边,另外一条小于等于最长边

    • (UIImage *)scaleToSize:(CGSize)newSize {
      CGFloat width = self.size.width;
      CGFloat height= self.size.height;
      CGFloat newSizeWidth = newSize.width;
      CGFloat newSizeHeight= newSize.height;
      if (width <= newSizeWidth &&
      height <= newSizeHeight) {
      return self;
      }

      if (width == 0 || height == 0 || newSizeHeight == 0 || newSizeWidth == 0) {
      return nil;
      }
      CGSize size;
      if (width / height > newSizeWidth / newSizeHeight) {
      size = CGSizeMake(newSizeWidth, newSizeWidth * height / width);
      } else {
      size = CGSizeMake(newSizeHeight * width / height, newSizeHeight);
      }
      return [self drawImageWithSize:size];
      }

    • (UIImage *)drawImageWithSize: (CGSize)size {
      CGSize drawSize = CGSizeMake(floor(size.width), floor(size.height));
      UIGraphicsBeginImageContext(drawSize);

      [self drawInRect:CGRectMake(0, 0, drawSize.width, drawSize.height)];
      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return newImage;
      }

    相关文章

      网友评论

          本文标题:ios中图片拉伸的几种方式

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