2011-04-21 122 views
1

其实我已经使用了以下两种方法进行图像大小调整,但我的客户说我的图像质量很差,所以我想知道图像大小调整的最佳方法。 我只是截取屏幕截图并将480x320大小的UIImage的截图发送到以下函数。调整图像大小而不损失质量的最佳方法是什么?

- (UIImage *)makeResizedImage:(CGSize)newSize quality:(CGInterpolationQuality)interpolationQuality image:(UIImage *)inImage{ 
    NSLog(@"OriginalImage%f",inImage.size.width); 

    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); 

    CGImageRef imageRef = [inImage CGImage]; 
    // Compute the bytes per row of the new image 
    size_t bytesPerRow = CGImageGetBitsPerPixel(imageRef)/CGImageGetBitsPerComponent(imageRef) * newRect.size.width; 
    bytesPerRow = (bytesPerRow + 15) & ~15; // Make it 16-byte aligned 

    // Build a bitmap context that's the same dimensions as the new size 
    CGContextRef bitmap = CGBitmapContextCreate(NULL, 
               newRect.size.width, 
               newRect.size.height, 
               CGImageGetBitsPerComponent(imageRef), 
               bytesPerRow, 
               CGImageGetColorSpace(imageRef), 
               CGImageGetBitmapInfo(imageRef)); 

    CGContextSetInterpolationQuality(bitmap, interpolationQuality); 

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

    // Get the resized image from the context and a UIImage 
    CGImageRef resizedImageRef = CGBitmapContextCreateImage(bitmap); 
    UIImage *resizedImage = [UIImage imageWithCGImage:resizedImageRef]; 
    NSLog(@"ResizedImage%f",resizedImage.size.width); 
    NSLog(@"ResizedImage%f",resizedImage.size.height); 

    // Clean up 
    CGContextRelease(bitmap); 
    CGImageRelease(resizedImageRef); 

    return resizedImage; 
} 

- (UIImage *)resizedImage:(CGSize)newSize 
       transform:(CGAffineTransform)transform 
      drawTransposed:(BOOL)transpose 
    interpolationQuality:(CGInterpolationQuality)quality { 
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); 
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width); 
    CGImageRef imageRef = self.CGImage; 

    // Build a context that's the same dimensions as the new size 
    CGContextRef bitmap = CGBitmapContextCreate(NULL, 
               newRect.size.width, 
               newRect.size.height, 
               CGImageGetBitsPerComponent(imageRef), 
               0, 
               CGImageGetColorSpace(imageRef), 
               CGImageGetBitmapInfo(imageRef)); 

    // Rotate and/or flip the image if required by its orientation 
    CGContextConcatCTM(bitmap, transform); 

    // Set the quality level to use when rescaling 
    CGContextSetInterpolationQuality(bitmap, quality); 

    // Draw into the context; this scales the image 
    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef); 

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

    // Clean up 
    CGContextRelease(bitmap); 
    CGImageRelease(newImageRef); 

    return newImage; 
} 

请告诉我,有没有其他的方法来调整图像大小,而不会失去其质量

+0

他们是如何失去质量?像素化,模糊,拉伸或其他? – blindjesse 2011-05-14 06:36:44

回答

相关问题