Save Image on iPhone idle time - iphone

I want to save the loaded image from server in idle time.I use the following to to save image in iPhone memory.
- (void)saveImage:(UIImage *)image withName:(NSString *)name
{
NSData *data1 = UIImageJPEGRepresentation(image, 1.0);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data1 attributes:nil];
if (image==nil) {
NSLog(#"Not Saved...:%#",name);
}
}
but when his method called i got halt time....i don't want that...i want to save image when iPhone is inactive.....can help??
Thanks

You need to do this in the background, using a thread.
If you're supporting iOS 4.0 only, it's really easy:
- (void)saveImage:(UIImage *)image withName:(NSString *)name {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
NSData *data1 = UIImageJPEGRepresentation(image, 1.0);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data1 attributes:nil];
if (image==nil) {
NSLog(#"Not Saved...:%#",name);
}
});
}
Otherwise, look up the NSThread documentation, or performSelectorInBackground:withObject:.

Related

How to copy one image and save as diff. name in document directory

In my document directory one image is their, with name Image1.
I want copy same image with name Image2 in document directory folder.
Please, suggest me how can i do that?
refer a following code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *imagePath1= [documentDirectory stringByAppendingPathComponent:#"image1.png"];
NSString *imagePath2= [documentDirectory stringByAppendingPathComponent:#"image2.png"];
[[NSFileManager defaultManager] copyItemAtPath:imagePath1 toPath:imagePath2 error:nil];
Get he data from first image and save the same data with another name like:
NSData *data = UIImageJPEGRepresentation(image1, 1.0);
NSString *str=#"image2.png"
NSString *fileName = [[stringPath stringByAppendingFormat:#"/"] stringByAppendingFormat:str];
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
[data writeToFile:fileName atomically:YES];
I have two mathed of Fatch image and save image:
You fatch image that save in Document directory and then pass it to save image with different name:
-(UIImage *)loadImage:(NSString *)name
{
NSString* documentsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
UIImage *res = [UIImage imageWithContentsOfFile:fullPath];
return res;
}
-(void)saveImage:(UIImage *)image withName:(NSString *)name
{
NSData *data = UIImageJPEGRepresentation(image, 1);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString* documentsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
}

Saving and loading UIImage in an iPhone app

I am having a headache working out what's wrong with my app. I am trying to save and load UIImages, simple right? Seems not so.
I have a class with these methods to save, load and delete an image with a unique string. Just how I like it, nice and simple. Here is the class:
imageSavedLoad.h:
#import <Foundation/Foundation.h>
#interface imageSaveLoad : NSObject
- (void)saveImage:(UIImage*)image forKey:(NSString*)imageName;
- (void)removeImage:(NSString*)fileName;
- (UIImage*)loadImage:(NSString*)imageName;
#end
imageSaveLoad.m:
#import "imageSaveLoad.h"
#implementation imageSaveLoad
//saving an image
- (void)saveImage:(UIImage*)image forKey:(NSString*)imageName
{
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", imageName]];
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
NSLog(#"image saved");
}
//removing an image
- (void)removeImage:(NSString*)fileName
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(#"image removed");
}
//loading an image
- (UIImage*)loadImage:(NSString*)imageName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", imageName]];
return [UIImage imageWithContentsOfFile:fullPath];
}
#end
Where I want to save, load or delete an image I import the class: #import "imageSaveLoad.h"
Then I create an instance of it and call the required methods (here I save):
imageSaveLoad *imgSL = [[imageSaveLoad alloc] init];
[imgSL saveImage:photoImgView.image forKey:#"yourPhoto"];
Then in another view/class where I want to retrieve the image:
imageSaveLoad *imgSL = [[imageSaveLoad alloc] init];
yourImgView.image = [imgSL loadImage:#"yourPhoto"];
But yourImgView displays nothing, blank.
So where am I going wrong? I have checked all IBOutlet connections and that all the code is being called. Is there a problem with the methods I am using?
When saving image, try by replacing this line :
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
With
[imageData writeToFile:fullpath atomically:YES];

Where does iPhone simulator put data files?

If I compile my iPhone App with a database that has been built into the bundle what path do I have to use to find the file so that I can open it, as in:
FMDatabase* db = [FMDatabase databaseWithPath:#"/???/database.sqlite"];
Inside your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
set the following code:
databaseName=#"yourfileName.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
-(void) checkAndCreateDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
else
printf("NO File found");
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
You can use like this regardless of simulator or device. Please dont forget to add db file to your project folder and add to your bundle.
#define DATABASE #"databasefile.sqlite"
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString:[documentsDir stringByAppendingPathComponent:DATABASE]];

iPhone Load and Save Multiple UIImages with Unique Key?

I know there are some solutions out there for saving and loading images on your iPhone. However none have quite worked for me.
For example, the link below has a great code chunk which lets you load and save a UIImage with a unique key, but I can't work out how to tell when loading an image using this code if an image under that key already exists or not.
http://www.friendlydeveloper.com/2010/02/using-nsfilemanager-to-save-an-image-to-or-loadremove-an-image-from-documents-directory-coding/
//saving an image
- (void)saveImage:(UIImage*)image:(NSString*)imageName {
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", imageName]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
NSLog(#"image saved");
}
//removing an image
- (void)removeImage:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(#"image removed");
}
//loading an image
- (UIImage*)loadImage:(NSString*)imageName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", imageName]];
return [UIImage imageWithContentsOfFile:fullPath];
}
If you could help me work out that out it would be really appreciated. I need to save several images, and load them depending on if a given key already exists or not.
if u want to get images with unique key, try using UUID. This will always give u image with unique key..For example here is an example of using UUID..
- (NSString *)pathForImageFileWithPrefix:(NSString *)Prefix
{
CFUUIDRef uuid;
CFStringRef uuidStr;
uuid = CFUUIDCreate(NULL);
uuidStr = CFUUIDCreateString(NULL, uuid);
NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
result = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_%#.png",Prefix, uuidStr]];
[imageData writeToFile:result atomically:NO];
CFRelease(uuidStr);
CFRelease(uuid);
return result;
}
If I get your question right, you want to know whether a file with the given image name already exists? Then you can just use
bool fileExists = [NSFileManager defaultManager] fileExistsAtPath:filePath];

Sharing multiple files on App Via iTunes File Sharing (Code shows 1 file)

I'm trying to file share multiple files with iTunes file share.
Here is the current code.
Delegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// file sharing trying
{
NSString *fileName = #"Test.mp3";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
if (![fileManager fileExistsAtPath:documentDBFolderPath])
{
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
}
}
[self.window makeKeyAndVisible];
return YES;
}
At the moment only 1 file gets shared.
Thanks
I assume you want to add a bunch of files in an NSArray? then loop through it like this:
NSArray *names = [NSArray arrayWithObjects: #"foo", #"bar", nil];
for (NSString *fileName in names)
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
if (![fileManager fileExistsAtPath:documentDBFolderPath])
{
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
}
}