2012-04-17 10 views
2

我想從JPG文件中獲取縮略圖並顯示爲UIImage。顯示樣式適合模式,比如UIViewContentModeScaleAspectFill。下面是示例代碼,但它太複雜了。如何繪製UIImage圖像,如擬合樣式

-(UIImage*)scaleToSize:(CGSize)size 
{ 
    CGFloat width = CGImageGetWidth(self.CGImage); 
    CGFloat height = CGImageGetHeight(self.CGImage);  

    NSLog(@"size w=%f, h=%f, image w=%f, h=%f", size.width, size.height, width, height); 

    float verticalRadio = size.height*1.0/height; 
    float horizontalRadio = size.width*1.0/width; 

    float radio = 1; 
    if(verticalRadio>1 && horizontalRadio>1) 
    { 
     radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;  
    } 
    else 
    { 
     radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;  
    } 

    width = width*radio; 
    height = height*radio; 
    NSLog(@"width=%f, height=%f", width, height); 

    int xPos = (size.width - width)/2; 
    int yPos = (size.height-height)/2; 

    NSLog(@"xpos=%d, ypos=%d", xPos, yPos); 

    CGSize sz = CGSizeMake(width, height); 
    UIGraphicsBeginImageContext(sz);  

    // 
    [self drawInRect:CGRectMake(0, 0, width, height)];  
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();  

    CGRect rt = CGRectMake(-xPos, -yPos, size.width, size.height); 
    UIImage* thub = [scaledImage getSubImage:rt]; 

    scaledImage = nil; 
    return thub; 
} 

    - (UIImage *)getSubImage:(CGRect) rect{ 
    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect); 
    CGRect smallBounds = CGRectMake(rect.origin.x, rect.origin.y,  CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef)); 

    NSLog(@"small bounds x=%f, y=%f, w=%f, h=%f", smallBounds.origin.x, smallBounds.origin.y, smallBounds.size.width, smallBounds.size.height); 

    UIGraphicsBeginImageContext(smallBounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextDrawImage(context, smallBounds, subImageRef); 
    UIImage* smallImg = [UIImage imageWithCGImage:subImageRef]; 
    UIGraphicsEndImageContext(); 

    return smallImg; 
} 
+0

'浮子無線電= 1; if(verticalRadio> 1 && horizo​​ntalRadio> 1) { radio = verticalRadio> horizo​​ntalRadio? horizo​​ntalRadio:verticalRadio; } else { radio = verticalRadio 2012-04-17 05:33:05

+0

後明確設置,這是測試代碼,我已經刪除它了。 thx – Golden 2012-04-17 09:46:20

+0

確定了它並將盡力爲您提供答案您正在尋找 – 2012-04-17 09:49:11

回答

3

我更新方法:

-(UIImage*)getSubImage:(CGSize)size 
{ 
    CGFloat width = CGImageGetWidth(self.CGImage); 
    CGFloat height = CGImageGetHeight(self.CGImage); 

    float verticalRadio = size.height*1.0/height; 
    float horizontalRadio = size.width*1.0/width; 

    float radio = 1; 
    radio = verticalRadio < horizontalRadio ? horizontalRadio : verticalRadio; 

    CGRect displayRect = CGRectMake((size.width - width*radio)/2.0f, 
           (size.height-height*radio)/2.0f, 
           width*radio, 
           height*radio); 


    // create a bitmap context and then set the context to current using  
    UIGraphicsBeginImageContext(size);  

    //clip 
    UIRectClip(displayRect); 

    //draw the bitmap in rect  
    [self drawInRect:displayRect];  

    // from context to get a UIIMage 
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  

    // move context out of buffer 
    UIGraphicsEndImageContext(); 

    NSLog(@"getSubImage ---->"); 

    // return. 
    return scaledImage; 
} 
+0

這是從jpg文件中獲得適合風格縮略圖的簡單方法。 – Golden 2012-04-18 00:32:16

相關問題