2011-12-03 36 views
6

Tôi đã thực hiện chức năng này ở bên dưới fitImage có chức năng UIImageCGSize. Trong trường hợp hình ảnh đầu vào lớn hơn thì hộp nhập liệu, hình ảnh sẽ được thu nhỏ lại để điền vào hộp. Nếu hộp và hình ảnh không có cùng tỷ lệ, hình ảnh sẽ không lấp đầy toàn bộ hộp và sẽ có một số nền có thể nhìn thấy được. Trong trường hợp này, nền này luôn có màu trắng. Làm cách nào tôi có thể thay đổi điều này thành ví dụ: một nền đen?UIImage: điền vào nền

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

Hãy cho tôi biết nếu bạn cần thêm thông tin.

Giải pháp:

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

Trả lời

22

Bạn có thể sử dụng UIRectFill(rect) để điền vào nền của một bối cảnh bitmap với màu fill hiện hành. Để đặt màu tô, bạn có thể sử dụng [Bộ màu].

ví dụ:

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

Cảm ơn bạn weichsel! – dhrm

Các vấn đề liên quan