views:

2202

answers:

3

To understand my question please go through following.

  • In my application user first taps on a button.
  • Image picker controller is displayed
  • user selects images / an image from it.
  • all that images must be saved to my iphone application.

I have already implemented this. & for doing this i have implemented following code.

-(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]; }

But the problem is - user's iphone may have larger images.

I don't want to store that large images within application. for example -> user selects an image having size of 1200 x 800 -> But i want only 80 x 80 size image

  1. selected images should be down sized to my requirement / 8 mb image to less then 500 kb
  2. how to store image within resource directory instead of storing in documents directory?

Thanks in advance for helping me.

Sagar.

+2  A: 

2) You can not store in the Resources directory now in any directory within the application.

zaph
@zaph - check out I have added my own answer. that works.
sugar
A: 
  1. any one can store in resource directory / up level directory of documents directory by implementing following logic.


-(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];
}
sugar
A: 
  1. Solved.

The main key for down sizing. I down sized an image up to 116 kb.


SData *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];
}
sugar