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]];
Related
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.
I am having a URL in which it contains images and audio files. I am downloading the images and audio files to document directory by using the below code:
// urlsArray contains the list of images and audio urls
for(int i= 0 ;i<[urlsArrray count]; i++)
{
NSString *urlString = [urlsArrray objectAtIndex:i];
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escapedUrlString];
NSData *fileData=[NSData dataWithContentsOfURL:url];
NSString *urlstring1 = [url absoluteString];
NSArray *parts = [urlstring1 componentsSeparatedByString:#"/"];
NSString *fileName = [parts objectAtIndex:[parts count]-1];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:fileName];
[fileManager createFileAtPath:savedImagePath contents:fileData attributes:nil];
NSLog(#"files Stored Path is %#",savedImagePath);
Now I want to seperate the images and audio files. I want to download images to document directory and audio files to cache directory.
Just add following code to recognize the file whether it is image or audio file
CFStringRef fileExtension = (CFStringRef) [file pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(#"It's an image");
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(#"It's a movie");
else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(#"It's text");
else NSLog(#"It's audio");
OK if you separate from your array then do as below:
imagelist = [[NSMutableArray alloc]init];
videolist = [[NSMutableArray alloc]init];
for(int i = 0;i<urlsArrray.count;i++)
{
id arrayElement = [fileArray objectAtIndex:i];
if([arrayElement rangeOfString:#".mp3" ].location !=NSNotFound)
{
[videolist addObject:arrayElement];
NSLog(#"videolist..%#",videolist);
}
else([arrayElement rangeOfString:#".png"].location !=NSNotFound)
{
[imagelist addObject:arrayElement];
NSLog(#"imagelist..%#",imagelist);
}
}
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.
I have a custom library that can pick mulitple images from the camera roll, I save my images in the NSDocumentDirectory like this:
for (int i = 0; i < info.count; i++) {
NSLog(#"%#", [info objectAtIndex:i]);
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"Images%d.png", i]];
ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
//----resize the images
image = [self imageByScalingAndCroppingForSize:image toSize:CGSizeMake(256,256*image.size.height/image.size.width)];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];
NSLog(#"saving at:%#",savedImagePath);
}
What I wanted to do is I wanted to load it in a thumbnail with UIScrollView then when a specific picture is tapped a AlertView will popup,will ask the user if it wants to delete it. I manage to make it load in a thumbnail view (I use theHSImageSidebarView) and make the AlertVIew popups whenever a user taps in a image. But when I press delete it deletes in the view but not in the NSDocumentDirectory.
This is how I delete my image:
- (void)sidebar:(HSImageSidebarView *)sidebar didRemoveImageAtIndex:(NSUInteger)anIndex {
NSLog(#"Image at index %d removed", anIndex);
[images removeObjectAtIndex:anIndex];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"Images%d.png", anIndex]];
[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(#"image removed");
}
On this line, replace %d with %lu
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"Images%d.png", anIndex]];
I am using a UIImagePickerController, and once an image is selected, I save it to a PNG. But for some reason, I can't load the saved image.
Here is my code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//
// Create a PNG representation of the edited image
//
UIImage *anImage = [info valueForKey:UIImagePickerControllerEditedImage];
NSData *imageData = UIImagePNGRepresentation(anImage);
//
// Save the image to the documents directory
//
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Get a unique filename for the file
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
NSString *fileName = [(NSString *)string autorelease];
fileName = [NSString stringWithFormat:#"images/%#", fileName];
fileName = [fileName stringByAppendingString:#".png"];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:fileName] ];
[imageData writeToFile:path atomically:YES];
NSLog(#"Wrote the imageData to a file at path: %#", path);
// TEST TO SEE IF THE FILE WAS WRITTEN CORRECTLY
UIImage *testImage = [UIImage imageWithContentsOfFile:path];
NSLog(#"testImage is %#", testImage);
And here is my debugger output:
2010-12-28 16:29:08.676 Appster[6656:207] The imagePath is defined as: images/7ADB104E-DA45-4EE9-88DA-FF71B8D730CA.png
2010-12-28 16:29:08.712 Appster[6656:207] Wrote the imageData to a file at path: /Users/bschiff/Library/Application Support/iPhone Simulator/4.2/Applications/9F6FC2C7-9369-438C-AF8F-D5D25C72D8D7/Documents/images/FC94AA93-02EC-492A-A8FE-7D0C76E2C085.png
2010-12-28 16:29:08.715 Appster[6656:207] testImage is (null)
Have you tried to use NSBundle to refer to your resources path?
Try this one:
[[NSBundle mainBundle] pathForResource:[NSString stringWithString:fileName]
ofType:#"png" inDirectory:#"images"]]
Use that path to save and retrieve the image file
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];