博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

http://www.iphonedevsdk.com/forum/iphone-sdk-development/10527-how-resize-image.html


I use several techniques depending on the situation. For most i resize the image to 640/480 which reduces the file size (and ultimately the transfer size) down with the code below. You could change the h/w to whatever you like. If you want to compress the image instead of resizing it give UIImageJPEGRepresentation a shot passing it a compression ratio value.

Hope that helps

Code:
+(UIImage*)compressImageDownToPhoneScreenSize:(UIImage*)theImage{

UIImage * bigImage = theImage;

float actualHeight = bigImage.size.height;
float actualWidth = bigImage.size.width;

float imgRatio = actualWidth / actualHeight;
float maxRatio = 480.0 / 640;

if( imgRatio != maxRatio ){
if(imgRatio < maxRatio){
imgRatio = 480.0 / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = 480.0;

} else {
imgRatio = 320.0 / actualWidth;
actualHeight = imgRatio * actualHeight; 
actualWidth = 320.0;
}

}


CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[bigImage drawInRect:rect]; // scales image to rect
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//RETURN
return theImage;

}


--------------


I also used Boston Merlin code, but it seems the final image is encoded in some weird order of ARGB.. something like ABGR or other. So, I modified it a bit to make it return a ARGB.

Code:
-(UIImage*)compressImageDownToPhoneScreenSize:(UIImage*)theImage{

UIImage * bigImage = theImage;



CGImageRef inImage = bigImage.CGImage;
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == NULL) { /* error */ }

size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);


float imgRatio = w / h;
float maxRatio = 640.0 / 480;

if( imgRatio != maxRatio ){
if(imgRatio < maxRatio){
imgRatio = 640.0 / h;
w = imgRatio * w;
h = 640.0;

} else {
imgRatio = 480.0 / w;
h = imgRatio * h; 
w = 480.0;
}

}


CGRect rect = {{0,0},{w,h}}; 

// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
CGContextDrawImage(cgctx, rect, inImage); 


CGImageRef imageRef = CGBitmapContextCreateImage (cgctx); 
UIImage* rawImage = [UIImage imageWithCGImage:imageRef]; 

//RETURN
return rawImage;

// When finished, release the context
CGContextRelease(cgctx);
// Free image data memory for the context



}
You also need this function (available in Apple doc as well, just cut and paste)
Code:
-(CGContextRef)createARGBBitmapContextFromImage:(CGImageRef)inImage {

CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void * bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;

// Get image width, height. We'll use the entire image.
size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);

// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);

// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateDeviceRGB(); 

if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space\n");
return NULL;
}

// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
fprintf (stderr, "Memory not allocated!");
CGColorSpaceRelease( colorSpace );
return NULL;
}

// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (bitmapData,
pixelsWide,
pixelsHigh,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
free (bitmapData);
fprintf (stderr, "Context not created!");
}

// Make sure and release colorspace before returning
CGColorSpaceRelease( colorSpace );

return context;
}

 

come from http://sue602.blog.163.com/blog/static/314953072010323112018803/ 

 

 

 

posted on 2012-11-30 23:17  Likwo  阅读(847)  评论(0编辑  收藏  举报