This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I wanted to save an image that has been taken to the application documents directory, but for some reason, the counter always going up but there are no picture inside the directory. what seems to be the problem?
Thanks alot!
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[picker dismissModalViewControllerAnimated:YES];
self.imageViewRecipt.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//
// Getting the current counter value
//
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
int imageCounter;
imageCounter = [[prefs objectForKey:#"imageCounter"]intValue];
//
// Obtaining saving path
//
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"image%i.png",imageCounter]];
//
// Extracting image from the picker and saving it
//
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"])
{
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
//
// Saving the new counter into the plist
//
imageCounter++;
[prefs setInteger:imageCounter forKey:#"imageCounter"];
[prefs synchronize];
}
}
OK, so I fixed it by removing the "mediaType" and the if statment and replace it with this:
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[picker dismissModalViewControllerAnimated:YES];
self.imageViewRecipt.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//
// Getting the current counter value
//
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
int imageCounter;
imageCounter = [[prefs objectForKey:#"imageCounter"]intValue];
//
// Obtaining saving path
//
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"image%i.png",imageCounter]];
//
// Extracting image from the picker and saving it
//
UIImage *editedImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
//
// Saving the new counter into the plist
//
imageCounter++;
[prefs setInteger:imageCounter forKey:#"imageCounter"];
[prefs synchronize];
}
and now it's working just fine.
Related
i want to get a photo from camera and using its name want to save in databsase...
i have used these code. i have successfully taken a picture and added in image view but now i can not able to get its name(string value)
ImagePicker = [[UIImagePickerController alloc] init];
ImagePicker.delegate = self;
ImagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:ImagePicker animated:YES];
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[ImagePicker dismissModalViewControllerAnimated:YES];
imageview.hidden=NO;
imageview.image = image;
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSString *dateString = [dateFormat stringFromDate:today];
// save image in document directoties
UIImage *image1=imageview.image;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSData *pngData = UIImageJPEGRepresentation(image1,100.0);
NSString *filePath;
filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"image%#.jpg",dateString]]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
NSLog(#"File Path is %#",filePath);
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) //Optionally check if folder already hasn't existed.
{
NSLog(#"Unable to create Folder in Documents Directory");
}
}
I guess knowing the exact image name would not be an issue rather getting a unique name for the picked image would solve your purpose so that you can save the image and track it via its name. May be this can help you
//Fetch the Maximum id from DB
NSString* strSelect= [NSString stringWithFormat:#"Select MAX(id) from TableName"];
NSMutableArray* arr_test = [[database executeQuery:strSelect]mutableCopy];
//Check if it is the first Image or not
if ([[[arr_test objectAtIndex:0]valueForKey:#"MAX(id)"]isKindOfClass:[NSNull class]])
img_id = 1;
else{
NSString *strtest = [[arr_test objectAtIndex:0] valueForKey:#"MAX(id)"];
img_id=[strtest intValue]+1;
}
//Give the Image Name with Unique ID
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/image%d.jpg",img_id]];
try
-(void)imagePickerController:(UIImagePickerController *)pickerr
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[pickerr dismissModalViewControllerAnimated:YES];
//Since we kept allowsEditing = YES , we use UIImagePickerControllerEditedImage else use UIImagePickerControllerOriginalImage
UIImage *image = [info objectForKey:#"UIImagePickerControllerEditedImage"];
[self saveImage:image];
}
(void)saveImage:(UIImage*)imagepk
{
Database *dbObj = [Database Connetion];
//NSLog(#"image width is %f",imagepk.size.width);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d.jpg",appDelegate.activeId]];
[dbObj updateImage:[NSString stringWithFormat:#"%d.jpg",appDelegate.activeId] :appDelegate.activeId];
UIImage *image = imagepk; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
hi friend i am saving image on document directry like this
in document directry the image is asaving like saveimage0.png,saveimage1.png...etc like this image is enter in document i create int variable for increment count with image num=0;
insert is working proper but how to get multiple image from document folder in loadimage menthod is right or wrong i want tkae image and save in array and display on view
int num=0;
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
UIImage *image1 =imageView.image;
NSData *myData = [UIImagePNGRepresentation(image1) retain];
imagedata = myData;
if (newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"saveimage%d.png",num]];
num += 1; // for next t
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
// Code here to support video if enabled
}
}
and i am fetching image from document directry same as
int value;
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString* path = [documentsDirectory stringByAppendingPathComponent:
//[NSString stringWithString: #"test.png"] ];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"savedImage-",#"%#-%d.png", value]];
value += 1;
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
Answer is within your question.
You are saving image using:
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"saveimage%d.png",num]];
The format is : saveimage0.png
And you are retrieving like:
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"savedImage-",#"%#-%d.png", value]];
The format is: savedImage-aValue-aValue.png.
Change it to:
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"savedimage%d.png", value]];
Trying to select image using photo picker and save that image internally in apps folder.
- (void) imagePickerController: (UIImagePickerController *) pickerdidFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToUse;
// Handle a still image picked from a photo album
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
== kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToUse = editedImage;
} else {
imageToUse = originalImage;
}
// Do something with imageToUse
//save it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageName = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:#"%d", myUniqueID]];
NSString *imagePath = [imageName stringByAppendingPathComponent:#".png"];
NSData *webData = UIImagePNGRepresentation(editedImage);
NSError* error = nil;
bool success = [webData writeToFile:imagePath options:NULL error:&error];
if (success) {
// successfull save
imageCount++;
[[NSUserDefaults standardUserDefaults] setInteger:imageCount forKey:#"imageCount"];
NSLog(#"#Success save to: %#", imagePath);
}
else if (error) {
NSLog(#"Error:%#", error.localizedDescription);
}
}
...
}
What I can't figure out is that writeToFile::: returns false but no value is returned in error so I can't figure out whats going wrong. Any help would be greatly appreciated thanks
You're missing a "/". The line:
NSString *imageName = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:#"%d", myUniqueID]];
should be:
NSString *imageName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d", myUniqueID]];
And the line that says:
NSString *imagePath = [imageName stringByAppendingPathComponent:#".png"];
should be:
NSString *imagePath = [imageName stringByAppendingPathExtension:#"png"];
Update:
And, shouldn't:
NSData *webData = UIImagePNGRepresentation(editedImage);
be the following?
NSData *webData = UIImagePNGRepresentation(imageToUse);
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 and 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 the 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
selected images should be down sized to my requirement / 8 mb image to less then 500 kb
how to store image within resource directory instead of storing in documents directory?
This is solved.
The main key for downsizing. I downsized an image up to 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];
}
2) You can not store in the Resources directory now in any directory within the application.
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];
}
I am using a UIImagePickerController in my application.
After selecting an image by user,
image should be saved at application Documents Directory,
my Code is Give Below.
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[picker dismissModalViewControllerAnimated:YES];
NSData *imgData = UIImagePNGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"]);
UIImage *img = [[UIImage alloc] initWithData:imgData];
stuImgView.image = img;
[img release];
//write image
NSString *imageFilename = [NSString stringWithFormat:#"%i.jpg", currentStudent.stuNo];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0], imageFilename];
UIImage *stuImg;
BOOL success;
NSFileManager *fm=[NSFileManager defaultManager];
// how to store file at documents dir????
}
i dont know how to use file manager to store a file?
Help me Out.
Thanks in advance.
You can use writeToFile:atomically::
[imgData writeToFile:path atomically:NO];