標籤:
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,};
demo:https://github.com/vitoziv/VICMAImageView
2. 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中圖片展開的幾種方式