2012-06-22 55 views
2

我是新来iPhone开发代码,我想搜索一些代码来调整我的UIImage到指定的大小,但保持比例。指定的大小就像一个图像不能跨越边界的框架,在该边界内图像应缩放以适合框架并保持比例,我目前使用的代码可以调整大小,但不能保留比率,只需粘贴在这里,看看我是否可以做一些微不足道的修改,使它成为可能。iPhone开发:任何片段来调整图像大小,并保持比例?

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize { 
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); 
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();  

return newImage; 

}

+0

你可以通过设置UIImageView的属性来做到这一点。不知道如何处理UIImage本身。 – nhahtdh

回答

3

好了,你可以调整图片的简单一点:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

然后,你需要计算一个新的大小保养的纵横比。 例如让你能提供这样创造了一个尺寸图像一半大小:用于调整图像大小,以特定的边界,同时保持纵横比

CGSize halfSize = CGSizeMake(image.size.width*0.5, image.size.height*0.5); 
2

伪代码:

imageSize // The image size, for example {1024,768} 
boundarySize // The boundary to fit the image into, for example {960,640} 

boundaryAspectRatio = boundarySize.width/boundarySize.height 
imageAspectRatio = imageSize.width/imageSize.height 

if (imageAspectRatio == boundaryAspectRatio) 
{ 
    // The aspect ratio is equal 
    // Resize image to boundary 
} 
else if (imageAspectRatio > boundaryAspectRatio) 
{ 
    // The image is wider 
    // Resize to: 
    // - Width: boundarySize.width 
    // - Height: boundarySize.height/imageAspectRatio 
} 
else if (imageAspectRatio < boundaryAspectRatio) 
{ 
    // Resize to: 
    // - Width: boundarySize.width * imageAspectRatio 
    // - Height: boundarySize.height 
} 
1

只要改变MAX到MIN以适合而不是填充;)

- (UIImage *)imageByFillingSize:(CGSize)newSize useScreenScale:(BOOL)useScreenScale 
{ 
    CGSize size = [self size]; 
    CGRect frame; 
    float ratioW = newSize.width/size.width; 
    float ratioH = newSize.height/size.height; 
    float ratio = MAX(ratioW, ratioH); 

    frame.size.width = size.width * ratio; 
    frame.size.height = size.height * ratio; 
    frame.origin.x = (newSize.width - frame.size.width)/2.0; 
    frame.origin.y = (newSize.height - frame.size.height)/2.0; 

    UIGraphicsBeginImageContextWithOptions(newSize, YES, useScreenScale ? 0.0 : 1.0); 
    [self drawInRect:frame]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 
    return newImage; 
}