在給view 添加背景圖片的時(shí)候,發(fā)現(xiàn)view 沒(méi)有 contents模式,這樣在給view添加背景圖時(shí)就變?cè)斐衫熳冃? 后來(lái)自己增加個(gè)類(lèi)目, 方便調(diào)用
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;
// 創(chuàng)建一個(gè)bitmap的context
// 并把它設(shè)置成為當(dāng)前正在使用的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)];
// 從當(dāng)前context中創(chuàng)建一個(gè)改變大小后的圖片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使當(dāng)前的context出堆棧
UIGraphicsEndImageContext();
// 返回新的改變大小后的圖片
return scaledImage;
}
//按大小縮放
-(UIImage*)scaleToFullSize:(CGSize)size
{
// 創(chuàng)建一個(gè)bitmap的context
// 并把它設(shè)置成為當(dāng)前正在使用的context
UIGraphicsBeginImageContext(CGSizeMake(size.width*3.0, size.height*3.0));
// 繪制改變大小的圖片
[self drawInRect:CGRectMake(0, 0, size.width*3.0, size.height*3.0)];
// 從當(dāng)前context中創(chuàng)建一個(gè)改變大小后的圖片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使當(dāng)前的context出堆棧
UIGraphicsEndImageContext();
// 返回新的改變大小后的圖片
return scaledImage;
}