2011-12-03 62 views
6

我在fitImage函数下面做了这个,它需要一个UIImage和一个CGSize。如果输入图像大于输入框,图像将被缩小以填充到框中。如果包装盒和图像的比例不同,则图像不能完全填充该框,并且会有一些可见的背景。在这种情况下,这个背景总是白色的。我该如何改变它黑色的背景?UIImage:填充背景

+ (UIImage*) fitImage:(UIImage*)image inBox:(CGSize)size { 

    if (image.size.width==size.width && image.size.height==size.height) 
     return image; 

    if (image.size.width<size.width && image.size.height<size.height) 
     return [Util scaleImage:image toSize:size]; 

    float widthFactor = size.width/image.size.width; 
    float heightFactor = size.height/image.size.height; 

    CGSize scaledSize = size; 
    if (widthFactor<heightFactor) { 
     scaledSize.width = size.width; 
     scaledSize.height = image.size.height * widthFactor; 
    } else { 
     scaledSize.width = image.size.width * heightFactor; 
     scaledSize.height = size.height; 
    } 

    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 

    float marginX = (size.width-scaledSize.width)/2; 
    float marginY = (size.height-scaledSize.height)/2; 
    CGRect scaledImageRect = CGRectMake(marginX, marginY, scaledSize.width, scaledSize.height); 

    [image drawInRect:scaledImageRect]; 

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    return scaledImage; 
} 

如果您需要更多信息,请告诉我。

解决方案:

+ (UIImage*) fitImage:(UIImage*)image inBox:(CGSize)size withBackground:(UIColor*)color { 

    if (image.size.width==size.width && image.size.height==size.height) 
     return image; 

    if (image.size.width<size.width && image.size.height<size.height) 
     return [Util scaleImage:image toSize:size]; 

    float widthFactor = size.width/image.size.width; 
    float heightFactor = size.height/image.size.height; 

    CGSize scaledSize = size; 
    if (widthFactor<heightFactor) { 
     scaledSize.width = size.width; 
     scaledSize.height = image.size.height * widthFactor; 
    } else { 
     scaledSize.width = image.size.width * heightFactor; 
     scaledSize.height = size.height; 
    } 

    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 

    float marginX = (size.width-scaledSize.width)/2; 
    float marginY = (size.height-scaledSize.height)/2; 
    CGRect scaledImageRect = CGRectMake(marginX, marginY, scaledSize.width, scaledSize.height); 

    UIImage* temp = UIGraphicsGetImageFromCurrentImageContext(); 
    [color set]; 
    UIRectFill(CGRectMake(0.0, 0.0, temp.size.width, temp.size.height)); 
    [image drawInRect:scaledImageRect]; 

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    return scaledImage; 
} 

回答

22

您可以使用UIRectFill(rect),以填补当前填充颜色的位图上下文的背景。要设置填充颜色,您可以使用[UIColor set]。

例如

//Get a temp image to determine the size of the bitmap context 
UIImage* temp = UIGraphicsGetImageFromCurrentImageContext(); 
[[UIColor redColor] set]; //set the desired background color 
UIRectFill(CGRectMake(0.0, 0.0, temp.size.width, temp.size.height)); //fill the bitmap context 
[image drawInRect:scaledImageRect]; //draw your image over the filled background 
+0

谢谢weichsel! – dhrm