Denali Creative
Denali Creative

Reputation: 55

Save Image With In App

What I am needing to do is, take a picture or choose one form the photo library, then save it within the app so that it isn't visible anywhere else but within the app. For example it would be like "My Secret Folder" where images are only seen within the app. I am not making a secret folder app.... So don't worry... =)

I am sorry I don't have much code to show, but I have no idea how to do this.

I was looking at the Rich Text File and was wondering if that was the way to go and if it can even store images, or if I have to do it a different way.

Thanks, Denali Creative LLC

P.S.

What I am looking to do is save MORE THAN ONE image within the application. So i will need to be able to name what the Image or what ever the image is saved into's file name.

Upvotes: 3

Views: 4815

Answers (7)

Fernando Cervantes
Fernando Cervantes

Reputation: 2962

I recommend that you store the images in the Documents Directory and read them from there as it is the most appropriate location to store app content.


Save Image to Documents Directory

-(void)saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
    } else {
        NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
    }
}

Load Image From Documents Directory

-(UIImage *)loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];
    return result;
}

How-To

//Definitions
NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

//Save Image to Directory
[self saveImage:imageFromURL withFileName:@"My Image" ofType:@"png" inDirectory:documentsDirectoryPath];

//Load Image From Directory
UIImage * imageFromWeb = [self loadImage:@"My Image" ofType:@"png" inDirectory:documentsDirectoryPath];

Upvotes: 0

Hiren
Hiren

Reputation: 12780

I think this idea may be help for you. You can create one app for that like with SQLIte. in which you save your images within your app. but the problem is the images which you save in your app and keep safe from others, you have to manually remove from image gallery

Upvotes: 0

Adnan Ahmad
Adnan Ahmad

Reputation: 447

use UIImagePickerController class and implement it delegate

There is a delegate method of image picker view delegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info ;

This method is get called when you select the picture from photo Library or take picture from camera.That is good to save your images in core data base then it is only visible with in your app.

Upvotes: 0

Jordan
Jordan

Reputation: 21770

  • Lookup/Search code for using UIImagePickerController
  • Convert Image to Data using Convert Image to Data
  • Save the data to the document folder. No other apps can access your documents folder. folder, see this post Save Image to Disk
  • Read data from disk, convert to image and display to reverse process.

Upvotes: 3

Rayfleck
Rayfleck

Reputation: 12106

Extract your image from the PhotoPicker info dictionary in the photo picker delegate callback.

Set up a subdirectory somewhere under your /Documents folder, and write the image there.

 NSData *pngData = UIImagePNGRepresentation(_myUIImage);
 BOOL successFlag = [pngData writeToFile:documentDirectorySubFolder options:0x0 error:&error];

Upvotes: 0

DivineDesert
DivineDesert

Reputation: 6954

So u dont want other app to recognize ur images. I am not sure but this might help you. Instead of standard png or jpg extention, just replace a dummy extention like .abc which other apps cant recognize. There might be other ways but I was using this method.

Upvotes: 0

Jesse Black
Jesse Black

Reputation: 7986

You can save the images within your application's Sandbox

The Documents folder is backup during syncs, and Library/Caches folder is not. That gives you a choice between levels of secrecy.

Once you have your image (UIImagePickerController and a class that follows <UIImagePickerControllerDelegate> protocol), convert it to NSData and archive it to your desired folder.

Something like

NSData * imageData = UIImagePNGRepresentation (image);
NSData * imageData = UIImageJPEGRepresentation (image);

when you unarchive the NSData, you can create the image with

[UIImage imageWithData:data];

Upvotes: 2

Related Questions