在iPhone应用程序中存储图像时压缩/缩小尺寸/调整图像大小

时间:2009-09-18 17:36:17

标签: iphone xcode uiimagepickercontroller

要了解我的问题,请按以下步骤操作:

  • 在我的应用程序中,用户首先点击一个按钮。
  • 显示图像选取器控制器
  • 用户从中选择图像/图像。
  • 所有图片必须保存到我的iPhone应用程序中。

我已经实现了这一点,为此,我实现了以下代码。

 -(IBAction)setPhoto:(id)sender {
    facPhotoPicker=[[UIImagePickerController alloc]init];
    facPhotoPicker.delegate=self;
    facPhotoPicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    facPhotoPicker.allowsImageEditing=YES;
    facPhotoPicker.navigationBar.barStyle=UIBarStyleBlackOpaque;
    [self presentModalViewController:facPhotoPicker animated:YES];
}

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
    NSData *imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
    UIImage *img=[[UIImage alloc] initWithData:imgData];
    facImgView.image=img;
    [img release];
    NSString *str=[NSString stringWithFormat:@"%i.jpg",[currentFaculty facultyNo]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], str];
    [imgData writeToFile:path atomically:YES];
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissModalViewControllerAnimated:YES];
}

但问题是用户的iPhone可能有更大的图像。

我不想在应用程序中存储那些大图像。 例如

  • 用户选择尺寸为1200 x 800
  • 的图像
  • 但我只想要80 x 80尺寸的图像

    1. 所选图像的尺寸应根据我的要求/ 8 mb图像小于500 kb
    2. 如何在资源目录中存储图像而不是存储在文档目录中?

3 个答案:

答案 0 :(得分:8)

这已经解决了。

缩小规模的主要关键。我将图像缩小到116 kb。


NSData *imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
while ([imgData length]>50000) { 
    compressionRatio=compressionRatio*0.5;
    imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
}

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
double compressionRatio=1;
NSData *imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
while ([imgData length]>50000) { 
    compressionRatio=compressionRatio*0.5;
    imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],compressionRatio);
}
UIImage *img=[[UIImage alloc] initWithData:imgData];
facImgView.image=img;
NSLog(@"%@",[info objectForKey:@"UIImagePickerControllerOriginalImage"],3);
[img release];


NSString *str=[NSString stringWithFormat:@"%i.jpg",[currentFaculty facultyNo]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [NSString stringWithFormat:@"%@", [paths objectAtIndex:0]];
path=[path stringByDeletingLastPathComponent];
path=[NSString stringWithFormat:@"%@/%@",path,str];
[imgData writeToFile:path atomically:YES];
[picker dismissModalViewControllerAnimated:YES];
}

答案 1 :(得分:2)

2)您现在无法在Resources目录中存储在应用程序的任何目录中。

答案 2 :(得分:0)

  1. 任何人都可以通过实现以下逻辑存储在文档目录的资源目录/上层目录中。

  2. -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
    {
    NSData *imgData=UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);
    UIImage *img=[[UIImage alloc] initWithData:imgData];
    facImgView.image=img;
    NSLog(@"%@",[info objectForKey:@"UIImagePickerControllerOriginalImage"],3);
    [img release];
    NSString *str=[NSString stringWithFormat:@"%i.jpg",[currentFaculty facultyNo]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [NSString stringWithFormat:@"%@", [paths objectAtIndex:0]];
    path=[path stringByDeletingLastPathComponent];
    path=[NSString stringWithFormat:@"%@/%@",path,str]; // storing path
    [imgData writeToFile:path atomically:YES]; // stores successfully 
    [picker dismissModalViewControllerAnimated:YES];
    }