UIImage高仿UIImageView 的contents的模式

在给view 添加背景图片的时候,发现view 没有 contents模式,这样在给view添加背景图时就变造成拉伸变形. 后来自己增加个类目, 方便调用

UIImage *image = [UIImage imageNamed:@"234.png"];

UIImage *imag2 = [image getSubImage:self.view.bounds withContentMode:UIViewContentModeScaleToFill];

self.view.layer.contents = (__bridge id _Nullable)(imag2.CGImage);



/**

//截取部分图片的模式

@param rect        需要填充视图的尺寸

@param contentMode 填充模式 支持三种 UIViewContentModeScaleToFill,UIViewContentModeScaleAspectFit,UIViewContentModeScaleAspectFill

@return 生成的新图片上

*/

-(UIImage*)getSubImage:(CGRect)rect withContentMode:(UIViewContentMode)contentMode;

{

CGRect  rectResult = CGRectMake(0, 0, 0, 0);

CGFloat imageHeigth =  self.size.height*self.scale;

CGFloat imageWeight =  self.size.width*self.scale;

CGFloat rectHeigth = rect.size.height;

CGFloat rectWeight = rect.size.width;

if (UIViewContentModeScaleAspectFit == contentMode) {

return  [self scaleToSize:CGSizeMake(rectWeight, rectHeigth)];

}

else if(UIViewContentModeScaleToFill == contentMode){

return [self scaleToFullSize:CGSizeMake(rectWeight, rectHeigth)];

}

else if (contentMode == UIViewContentModeScaleAspectFill) {

float verticalRadio = rectHeigth/imageHeigth;

float horizontalRadio = rectWeight/imageWeight;

float radio = 1;

if(verticalRadio>1 && horizontalRadio>1)

{

radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;

}

else

{

radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;

}

if (radio == horizontalRadio) {

rectResult = CGRectMake((imageWeight-imageHeigth*rectWeight/rectHeigth)/2,0 , imageHeigth*rectWeight/rectHeigth, imageHeigth);

}

else if(radio == verticalRadio) {

rectResult = CGRectMake(0, (imageHeigth-imageWeight*rectHeigth/rectWeight)/2, imageWeight, imageWeight*rectHeigth/rectWeight);

}

else{

rectResult = CGRectMake(0, 0, imageWeight, imageHeigth);

}

}

CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rectResult);

CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));

UIGraphicsBeginImageContext(smallBounds.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextDrawImage(context, smallBounds, subImageRef);

UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];

UIGraphicsEndImageContext();

return smallImage;

}

//等比例缩放

-(UIImage*)scaleToSize:(CGSize)size

{

CGFloat width = CGImageGetWidth(self.CGImage);

CGFloat height = CGImageGetHeight(self.CGImage);

float verticalRadio = size.height*1.0/height;

float horizontalRadio = size.width*1.0/width;

float radio = 1;

if(verticalRadio>1 && horizontalRadio>1)

{

radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;

}

else

{

radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;

}

width = width*radio;

height = height*radio;

int xPos = (size.width - width)/2;

int yPos = (size.height-height)/2;

// 创建一个bitmap的context

// 并把它设置成为当前正在使用的context

UIGraphicsBeginImageContext(CGSizeMake(size.width*3.0, size.height*3.0));

// 绘制改变大小的图片

[self drawInRect:CGRectMake(xPos*3.0, yPos*3.0, width*3.0, height*3.0)];

// 从当前context中创建一个改变大小后的图片

UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

// 使当前的context出堆栈

UIGraphicsEndImageContext();

// 返回新的改变大小后的图片

return scaledImage;

}

//按大小缩放

-(UIImage*)scaleToFullSize:(CGSize)size

{

// 创建一个bitmap的context

// 并把它设置成为当前正在使用的context

UIGraphicsBeginImageContext(CGSizeMake(size.width*3.0, size.height*3.0));

// 绘制改变大小的图片

[self drawInRect:CGRectMake(0, 0, size.width*3.0, size.height*3.0)];

// 从当前context中创建一个改变大小后的图片

UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

// 使当前的context出堆栈

UIGraphicsEndImageContext();

// 返回新的改变大小后的图片

return scaledImage;

}

推荐阅读更多精彩内容