- UIImageView整體拉伸
UIImageView-contentMode
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill, // 默認(rèn) 拉伸(會(huì)變形)
UIViewContentModeScaleAspectFit, // 等比例拉伸
UIViewContentModeScaleAspectFill, // 等比例填充
UIViewContentModeRedraw, // redraw on bounds change (這個(gè)不清楚)
UIViewContentModeCenter, // 下面的就是不拉伸按位置顯示了
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
- UIImage局部拉伸
// 按4邊間距顯示不拉伸的區(qū)域
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0);
// 按2點(diǎn)拉伸 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
// 拉伸模式
typedef NS_ENUM(NSInteger, UIImageResizingMode) {
UIImageResizingModeTile,//進(jìn)行區(qū)域復(fù)制模式拉伸
UIImageResizingModeStretch,//進(jìn)行漸變復(fù)制模式拉伸
};
3.UIImage修改大小
//內(nèi)縮放,一條變等于最長(zhǎng)邊经备,另外一條小于等于最長(zhǎng)邊
-
(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;
}