Creating an app's own Photo Gallery (iOS) - ios

Hello I have an application that takes pictures and displays the newly taken pictures to the image view. I can save the image and load it again. It is saved in the app's directory. Is it possible to create a album just for the use of that app? The pictures shouldn't be seen in the iPhone's photo gallery for security purposes. If it is possible, can anyone explain to me how to do it? Here are my current codes.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
UIImagePickerController *picker;
UIImagePickerController *picker2;
UIImage *image;
IBOutlet UIImageView *imageView;
}
-(IBAction)TakePhoto;
-(IBAction)ChooseExisting;
- (IBAction)btnSave:(id)sender;
- (IBAction)btnLoad:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)TakePhoto{
picker = [[UIImagePickerController alloc]init];
picker.delegate=self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
// [picker release];
}
- (void)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.png"]; //Add the file name
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
image = [UIImage imageWithData:pngData];
}
-(IBAction)ChooseExisting{
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate=self;
[picker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker2 animated:YES completion:NULL];
// [picker release];
}
- (NSString *)documentsPathForFileName:(NSString *)name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
return [documentsPath stringByAppendingPathComponent:name];
}
- (IBAction)btnSave:(id)sender {
NSData *pngData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
}
- (IBAction)btnLoad:(id)sender {
[self loadImage];
[imageView setImage:image];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad
{
[self loadImage];
[imageView setImage:image];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Related

Setting Image from gallery in iOS

I select an image from gallery and display on UIImageView but when I click back button and again open push to the image view controller that image is empty.
What should I do to remain same image on UIImageView after logout also?
I have used this code:
- (void)viewDidLoad {
NSString *myGrrabedImage1=#"myGrrabedImage1.png";
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1=[path objectAtIndex:0];
NSString *fullPathToFile1=[documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
NSData *data=[NSData dataWithContentsOfFile:fullPathToFile1];
[[self teacherImg]setImage:[UIImage imageWithData:data]];
[data writeToFile:fullPathToFile1 atomically:YES];
}
- (IBAction)selectImg:(id)sender {
pickerController = [[UIImagePickerController alloc]init];
pickerController.delegate = self;
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
UIImage * img = [info valueForKey:UIImagePickerControllerEditedImage];
teacherImg.image = img;
[self presentViewController:pickerController animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
NSData *data=UIImagePNGRepresentation(teacherImg.image);
NSString *myGrrabedImage1=#"myGrrabedImage1.png";
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1=[path objectAtIndex:0];
NSString *fullPathToFile=[documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
[data writeToFile:fullPathToFile atomically:YES];
imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
[[self teacherImg]setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}
In viewDidLoad() method you need to load image from doc. dir.
NSString *myGrrabedImage1 = #"myGrrabedImage1.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1 = [path objectAtIndex:0];
NSString *fullPathToFile = [documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
Method 1:
NSData *imgData = [NSData dataWithContentsOfFile: fullPathToFile];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
Method 2:
UIIMage *image = [UIImage imageWithContentsOfFile: fullPathToFile];
[imgView setImage: image];
You should read the image data and set the ImageView image in the viewWillAppear method.
viewDidLoad is called only once, after the view has been loaded from the xib/storyboard
Try this way;
- (void)viewDidLoad {
NSString *myGrrabedImage1 = #"myGrrabedImage1.png";
NSArray *path =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1 = [path objectAtIndex:0];
NSString *pathFile = [documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
UIIMage *image = [UIImage imageWithContentsOfFile: pathFile];
[teacherImg setImage: image];
}
- (IBAction)selectImg:(id)sender {
pickerController = [[UIImagePickerController alloc]init];
pickerController.delegate = self;
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pickerController animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
UIImage * img = [info valueForKey:UIImagePickerControllerEditedImage];
teacherImg.image = img;
NSData *data=UIImagePNGRepresentation(teacherImg.image);
NSString *myGrrabedImage1=#"myGrrabedImage1.png";
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1=[path objectAtIndex:0];
NSString *fullPathToFile=[documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
[data writeToFile:fullPathToFile atomically:YES];
[[self teacherImg]setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}

Save image to Documents directory than load it into UIImageView

I have been trying to solve this issue since days. I have an app where the user selects an image than the selected image is saved to the disk and later loaded into a UIImageView, however this does not work, no matter what I try. i have made a small app just to test this, and it does not work there either. The imageView stays blank. Here is my code:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)button:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *selectedImage = info[UIImagePickerControllerOriginalImage];
[self saveImage:selectedImage];
self.imageView.image = [self loadImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)saveImage: (UIImage*)image
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
#"image.png" ];
NSData* data = UIImagePNGRepresentation(image);
[data writeToFile:path atomically:YES];
}
- (UIImage*)loadImage
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
#"image.png" ];
UIImage* image = [UIImage imageWithContentsOfFile:path];
return image;
}
#end
What am I doing wrong?
You just need to add the delegate of the UIImagePickerController so that following code solve you problem.
- (IBAction)button:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.allowsEditing = NO;
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];
}
Now, In .h file use following code.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
#property(nonatomic,weak)IBOutlet UIImageView *imageView;
#end

How to Save Image to Application Folder from UIImagePickerController?

I've a question in mind that driving me crazy. I've searched a lot to solve it, but all the answers that I found were old and not helped.
I'm currently working on application with that uses UIImagePickerControllerDelegate and with didFinishPickingMediaWithInfo I want to save the selected image to application folder.
Until now I'm able to select the image with this;
- (IBAction)btnPressed:(UIButton *)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
imgPicker.allowsEditing = NO;
[self presentViewController:imgPicker animated:YES completion:nil];
}
}
but couldn't save it to application using didFinishPickingMediaWithInfo;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *) kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//Assigning the selected image to imageView to see on UI side.
imageViewerImage.image = image;
}
}
I wonder what should be the part after this point.
I appreciate any help that you can provide.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:NULL];
UIImage* image;
if([[info valueForKey:#"UIImagePickerControllerMediaType"] isEqualToString:#"public.image"])
{
image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:#"New Folder"];
// New Folder is your folder name
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *fileName = [stringPath stringByAppendingFormat:#"/image.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
[data writeToFile:fileName atomically:YES];
}
}
Try this code. It may helps you. :)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
imgViewProfile.image = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:^{ }];
// ------ Now save this image to document directory by getting its path
}
Save an UIImage to the to Documentsfolder like this:
UIImage* myUIImage = ...
NSData *pngData = UIImagePNGRepresentation(myUIImage);
//NSData *pngData = UIImageJPEGRepresentation(myUIImage,0.5); //alternative comressed jpg instead of png
NSString *filePath = [[self buildDocumentsPath] stringByAppendingPathComponent:#"pictureName.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
Get the path of the documents as a string:
- (NSString *)buildDocumentsPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
return documentsPath;
}
Just save the image in your app document directory :
// first find the path in which to save the image :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *baseDir = [paths objectAtIndex:0];
// then save image as JPEG
NSString* filename = [baseDir stringByAppendingPathComponent:#"filename.jpg"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
// or PNG
NSString* filename = [baseDir stringByAppendingPathComponent:#"filename.png"];
[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
//In Your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage (UIImage *)image editingInfo:(NSDictionary *)editingInfo . Write this code
NSData *pngData = UIImagePNGRepresentation(image);
//This pulls out PNG data of the image you've captured. From here, you can write it to a file:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.png"]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
I did that in my project:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage * imageDone = [info objectForKey:#"UIImagePickerControllerEditedImage"];
[self saveImage:imageDone];
[self.popover dismissPopoverAnimated:YES];
}
-(void)saveImage:(UIImage*)image{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString * imgpath = [documentsDirectory stringByAppendingFormat:#"/media/img/"];
long x = arc4random() % 10000;
NSString * namePic = [NSString stringWithFormat:#"%lld%ld-photo.jpg", self.idToLoad.longLongValue, x];
NSString * stringName = [NSString stringWithFormat:#"%#%#", imgpath, namePic];
[UIImageJPEGRepresentation(image, 0.3) writeToFile:stringName atomically:YES];
}

Save Imagedata in the imageview

In the app i made an image view that holds the Image chosen from the library of the phone or taken by the camera. But when I go back to the previous scene the image chosen is gone. I want it to be saved in the image view and have a clear button to remove it. The camera function works but the image doesn't stay in the image view.
The camera function:
.h file
#interface FMEImageView : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
IBOutlet UIImageView *ImageView;
UIImagePickerController *picker;
UIImage *image;
}
- (IBAction)Takephoto:(id)sender;
- (IBAction)Chosenphoto:(id)sender;
#end
.m file
- (IBAction)Takephoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)Chosenphoto:(id)sender{
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[ImageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
}
I think, you will have to save images to the documents directory of your application.
Get image data.
NSData *imgData = UIImagePNGRepresentation(image);
Write it to documents directory.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.png"];//Choose name for the image.
[imgData writeToFile:filePath atomically:YES]; //Write the file
Get the data and display it on the imageView.
Again, get filePath.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"image.png"];//Name you have given
NSData *imgData = [NSData dataWithContentsOfFile:filePath];
UIImage *image = [UIImage imageWithData:imgData];
imageView.image = image;
It is better to make a function to get the path of documents directory.
I hope it helps.
You might be using new instance every time. use same instance once initialized.

Saving image from camera to documents directory

I have the following code which launches the camera app and the user can select "use photo". However nothing happens.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePickerController animated:YES];
}
How I can I get an UIimage from this and save it to the photo album. Ideally I'd like to save this to the documents directory and know how to do that part. Thanks!
-(void) imagePickerController:(UIImagePickerController *)UIPicker didFinishPickingMediaWithInfo:(NSDictionary *) info
{
UIImage* originalImage = nil;
originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerCropRect];
}
[addImageOutlet setImage:originalImage forState:UIControlStateNormal];
[self saveBackgroundImageInDocumentDirectory:originalImage];
[self dismissViewControllerAnimated:YES completion:nil];
}
Use this code:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData * imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
[imageData writeToFile:savedImagePath atomically:NO];
[picker dismissViewControllerAnimated:YES completion:nil];
}
I think it will help you,
//you have to set the delegate which clicking the button of camera
imagePickerController.delegate = self;
//This Delegate method will call
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage* selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSString *path = [[self pathToPatientPhotoFolder] stringByAppendingPathComponent:[NSString stringWithFormat:#"imageName.png"]];
NSError * error11 = nil;
[post.getData writeToFile:path options:NSDataWritingAtomic error:&error11];
}
//This method is useful for the get the Documentry path
- (NSString *)pathToPatientPhotoFolder {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *myalbumpath = [documentsDirectory stringByAppendingPathComponent:#"MYAlbum"];
// Create the folder if necessary
BOOL isDir = NO;
NSFileManager *fileManager = [[NSFileManager alloc] init];
if (![fileManager fileExistsAtPath:myalbumpath
isDirectory:&isDir] && isDir == NO) {
[fileManager createDirectoryAtPath:myalbumpath
withIntermediateDirectories:NO
attributes:nil
error:nil];
}
return myalbumpath;
}
Enjoy the coding
addImageOutlet for what type of object

Resources