UIImage:填充背景

时间:2011-12-03 09:28:41

标签: objective-c uiimage

我在fitImage函数下面创建了UIImageCGSize。如果输入图像大于输入框,则图像将按比例缩小以填充到框中。如果框和图像的比例不同,则图像不会完全填满框,并且会有一些可见的背景。在这种情况下,此背景始终为白色。如何将其更改为例如黑色背景?

+ (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;
}

1 个答案:

答案 0 :(得分:22)

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

e.g。

//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