2015-03-02 142 views
1
- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize { 
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); //CGSize is 51*51 
CGImageRef imageRef = image.CGImage; 

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

// Set the quality level to use when rescaling 
CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height); 

CGContextConcatCTM(context, flipVertical); 
// Draw into the context; this scales the image 
CGContextDrawImage(context, newRect, imageRef); 

// Get the resized image from the context and a UIImage 
CGImageRef newImageRef = CGBitmapContextCreateImage(context); 
UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

CGImageRelease(newImageRef); 
UIGraphicsEndImageContext(); 
NSLog(@"size of resizeimage is %@",NSStringFromCGSize(newImage.size)); // here i get 102*102 
return newImage; 
} 

我進入cgsize是51 * 51,調整大小的UIImage沒有給我確切的新的大小調整後

和調整後,當我檢查的大小及其給我102 * 102。

爲什麼?請解決我的問題。

+0

ü嘗試搜索烏爾問題的答案嗎? http://stackoverflow.com/a/2658801/887325 – Bimawa 2015-03-02 12:09:51

+0

大小51 * 51是指非視網膜版本的圖像。你應該使用視網膜顯示,因此,你會得到2倍的價值。這在技術上是正確的。 – 2015-03-02 12:24:49

+0

非常感謝Jasmeet Singh。你節省了我的時間。非常感謝!!! – MAC113 2015-03-03 03:52:03

回答

1

看看你UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);,從蘋果文檔的尺度參數的意思是:

The scale factor to apply to the bitmap. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen. 

所以,如果它是一個視網膜顯示器,這是正常的。

0

我按照這個方法來調整圖像

-(UIImage *)resizeImage:(UIImage *)image newSize:(CGSize)newSize 
{ 
    float actualHeight = image.size.height; 
    float actualWidth = image.size.width; 
    float maxHeight = newSize.height; 
    float maxWidth = newSize.width; 
    float imgRatio = actualWidth/actualHeight; 
    float maxRatio = maxWidth/maxHeight; 

    if (actualHeight > maxHeight || actualWidth > maxWidth) 
    { 
     if(imgRatio < maxRatio) 
     { 
     //adjust width according to maxHeight 
     imgRatio = maxHeight/actualHeight; 
     actualWidth = imgRatio * actualWidth; 
     actualHeight = maxHeight; 
     } 
     else if(imgRatio > maxRatio) 
     { 
     //adjust height according to maxWidth 
     imgRatio = maxWidth/actualWidth; 
     actualHeight = imgRatio * actualHeight; 
     actualWidth = maxWidth; 
     } 
     else 
     { 
     actualHeight = maxHeight; 
     actualWidth = maxWidth; 
     } 
    } 

    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight); 
    UIGraphicsBeginImageContext(rect.size); 
    [image drawInRect:rect]; 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


    NSLog(@"size of resizeimage is %@",NSStringFromCGSize(img.size)); 
    NSLog(@"size of original is %@",NSStringFromCGSize(image.size)); 


    return img; 

    } 
相關問題