2011-12-03 37 views
6

Aşağıdaki UIImage ve bir CGSize alır fitImage işlevi aşağıda yaptım. Giriş görüntüsünün giriş kutusundan daha büyük olması durumunda, görüntü kutunun içine doldurulacak şekilde küçültülecektir. Kutu ve görüntü aynı orana sahip değilse, görüntü kutuyu tamamen doldurmaz ve görünür arka plan olur. Bu durumda, bu arka plan her zaman beyazdır. Bunu, ör. siyah bir arka plan?UIImage: dolgu arka plan

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

Lütfen daha fazla bilgiye ihtiyacınız olup olmadığını bana bildirin.

Çözüm:

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

cevap

22

Geçerli dolgu rengiyle bir bitmap bağlamda arka planını doldurmak için UIRectFill(rect) kullanabilirsiniz. Dolgu rengini ayarlamak için [UIColor set] 'i kullanabilirsiniz.

örn.

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

Teşekkür ederiz weichsel! – dhrm