如何在iphone sdk中压缩相机拍摄的图像?

Mon*_*mar 5 objective-c

我从地址簿中获取联系人列表,其中一些联系人的图像是用相机拍摄的,它们的尺寸非常大.我正在以3x3行和列格式显示联系人及其图像.问题是由于大尺寸的图像花费时间加载图像.任何人都建议我如何压缩它们.我尝试以某种方式压缩它们:

if ([imageData length] > 0)
{
    int len = [imageData length];
    if(len > 9000)
    {
        UIImage *theImage =  [UIImage imageWithData:imageData];
        imageData = UIImageJPEGRepresentation(theImage,0.5);
        printf("\n image data length in condition...%d",[imageData length]);
        imageViewL.image = [UIImage imageWithData:imageData];
    }
    else
    {
        imageViewL.image = [UIImage imageWithData:imageData];   
    }
}
Run Code Online (Sandbox Code Playgroud)

尽管需要花时间加载.

任何人的帮助将不胜感激.

感谢所有人,Monish.

Atu*_*ain 11

您可以使用以下代码行调整iphone相机捕获的图像大小

-(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize
{  
    UIGraphicsBeginImageContext(newSize);  
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];  
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();    
    return newImage;  
}
Run Code Online (Sandbox Code Playgroud)

然后像这样调用这个方法

UIImage *image = [self scaleImage:your image toSize:CGSizeMake(320.0,480.0)];
Run Code Online (Sandbox Code Playgroud)