2011-12-03 13 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) का उपयोग वर्तमान भरण रंग के साथ एक बिटमैप संदर्भ की पृष्ठभूमि को भरने के लिए कर सकते हैं। भरने के रंग को सेट करने के लिए आप [यूआईसीओलर सेट] का उपयोग कर सकते हैं।

उदा

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

धन्यवाद वीचसेल! – dhrm