Link a picture taken on my app [closed] - iphone

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
On my app, I have a button that is calling the Camera. I want to, after user takes the picture, save this image to the user's library and link this image with my app (like an ID).
There is a way to do this without saving the image on my databae?

Yes absolutely you can do that!
I recently uploaded a project on Github called "Chanda" where I demonstrate that usage. Please visit the following URL and check out the complete code:
https://github.com/azamsharp/Chanda
Here is the snippet of code but I highly recommend that you download the project and see it in action.
+(NSString *) getUniqueIdentifier
{
CFUUIDRef uuid;
CFStringRef uuidStr;
uuid = CFUUIDCreate(NULL);
uuidStr = CFUUIDCreateString(NULL, uuid);
return (__bridge NSString *)(uuidStr);
}
// user generated images are stored in the documents directory
+(UIImage *) loadImageFromDocumentsDirectory:(NSString *)imageUrl
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",imageUrl]];
UIImage *image = [UIImage imageWithContentsOfFile:savedImagePath];
return image;
}
// save user's image into the documents directory
+(NSString *) saveImageIntoDocumentsDirectoryAndReturnPath:(UIImage *)imageToSave
{
imageToSave = [imageToSave imageByScalingProportionallyToSize:CGSizeMake(320, 480)];
NSString *uniqueImageName = [self getUniqueIdentifier];
uniqueImageName = [uniqueImageName stringByAppendingPathExtension:#"png"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",uniqueImageName]];
NSData *imageData = UIImagePNGRepresentation(imageToSave);
[imageData writeToFile:savedImagePath atomically:YES];
return uniqueImageName;
}

I saved the image's path to database, following this tutorial: http://blog.teliaz.com/2010/capture-saveloadremove-image-in-documents-directory
And save the image to the photo library:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

Related

Xcode-Saving the image from the photo library for further use

I am trying to make a simple application using XCode 4.5 which would allow the user to chose any particular image via accessing his photo library, then submit that image for recognition with the help of the Tesseract library.
The problem is that I do not know how to further save the selected user picture, In other words, I can provide the user with an option for going in the picture library and let him chose a picture, but I do not how to then save that selected picture so that it can be further processed.
I hope i made it clear, Appreciate your help.
Try this,
Save image:
NSString *imgName=[#"imgname.png"];
[[NSUserDefaults standardUserDefaults]setValue:imgName forKey:#"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
Retrive image:
NSString *imgName= [[NSUserDefaults standardUserDefaults]valueForKey:#"imageName"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *filePath=[NSString stringWithFormat:#"%#/%#",documentsDir,imageName];
[imageview setImage:[UIImage imageWithContentsOfFile:filePath]];
What you need to do is save the image data after selection inside the following delegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { You can get the image via the key UIImagePickerControllerOriginalImage in the info dictionary and possibly assign it to an imageview or save it as mentioned by #Kalpesh. Check out apple's documentation for more info. Also check out this good tutorial that shows how to pick photos from the photo album or the camera.
I am still not clear about what exactly you want.
You can always Save Image in or Retrieve Image from Photo Library.

Store picture take in the app directory and give a custom name to it. [closed]

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 creating a camera app for iphone, and I am facing one issue.
I want to be able to give the picture I take, to be stored in the app's private directory and also to give picture a custom name to it (certainly via code). Eg I took a pic, I want to name the pic as mmddyyyyhhmmssXYZ and store thing image in myApp/images directory.
Please let me know if this is possible. And if it is possible, any suggestion or tutorial regarding would be very helpful.
Being new to iphone app development... I am actually clueless about this right now.
Thanks
Here is my code see Below.
-(void)copyImagesToCache:(NSString*)imageURl
{
NSFileManager *filemanager=[NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:#"MyIMages"];
//now no need to use below single line of code.
//NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"image.png"];
//it just a example here , here you should make a source path as you pic the images form the camera.
EDIT:While Taking pick in ImagePicker you should make a path . see below delegate of ImagePickerViewControllerDelegate.i have shown the way
if(![filemanager contentsOfDirectoryAtPath:folderPath error:nil]){
[filemanager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *destPath = [folderPath stringByAppendingPathComponent:#"image1.png"];//here you should create the dynamic name so that you can distinguish all images.
NSError *err;
BOOL isCopeid= [filemanager copyItemAtPath:srcPath toPath:destPath error:&err];
if(isCopeid)
NSLog(#"Copied");
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
myIMage.image = image;
//here call the method whcih copy the image to cache
[self copyImagesToCache: [editingInfo objectForKey:UIImagePickerControllerReferenceURL]]];
[here is the link which shown the editingInfo's keys ][1]
//remaining code goes here as it is
}
I hope it may help you...!!!!
Function to write your image to the app's private directory-
-(void)writeImagetoPath
{
UIImage *myImage = //This is the UIImage you obtained from the camera or the photo library.
//This is the path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"xyz.png",i]];//You can give any name to the path.
//Now convert the uiimage to data before writing to file.
NSData *imgData = UIImagePNGRepresentation(myImage);
[imgData writeToFile:documentsPath atomically:YES];
}

Beginner in iphone ,Making a pdf,what am i doing wrong

I am making a pdf app in which i get problem
// make pdf
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
BOOL isFileCreated;
if(isPortrait)
{
isFileCreated = UIGraphicsBeginPDFContextToFile([NSString stringWithFormat:#"%#/test.pdf",documentsDirectory],CGRectMake(self.m_ScrollView.frame.origin.x, self.m_ScrollView.frame.origin.y-340, self.m_ScrollView.frame.size.width, self.m_ScrollView.contentSize.height), nil);
}
else
{
isFileCreated = UIGraphicsBeginPDFContextToFile([NSString stringWithFormat:#"%#/test.pdf",documentsDirectory],CGRectMake(self.m_ScrollView.frame.origin.x, self.m_ScrollView.frame.origin.y-250, self.m_ScrollView.frame.size.width, self.m_ScrollView.contentSize.height), nil);
}
if(isFileCreated)
{
UIGraphicsBeginPDFPage();
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndPDFContext();
}
this code creates only half pdf. which currently shows on the screen and rest of the pdf is missing which is currently not shown on screen.what should i do?
thanks in advance.
http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/16552-pdf-viewer-tutorial.html
or
iOS SDK - Programmatically generate a PDF file

iphone sdk -Loading PDF from document directory

I'm Using Leaves project to show my PDFs in my iphone project but i have a problem when i'm trying to display the pdf from the document not from bundle .. i think it's simple but have a trick i can't get cause i'm not guru in pdf reading :)
i'm using the following code
-(void) viewWillAppear:(BOOL)animated{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
pListPath = [ documentDir stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",self.fileName]];
CFURLRef pdfURL = (CFURLRef)[NSURL fileURLWithPath:pListPath];
// CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("fekhElSunaI.pdf"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
NSLog(#"%#",pListPath);
// CFRelease(pdfURL);
[self init];
}
you'll notice that i've put the code in the viewWillAppear not in the init as leaves do ..
the pdf is not appear and not initialized
i've used the solution that is on this LINK and it didn't work either
so any one here have something to help me with :)
You've put the code in viewWillAppear, and that's probably your issue. Elsewhere the code might assume the code is already called, and the PDF already there. Try putting it in init, and see what happens. viewWillAppear might get called several times.
Use this code
- (id)init {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
myPathDocs = [documentsDirectory stringByAppendingPathComponent:#"abc.pdf"];
pdfURL = (CFURLRef)[NSURL fileURLWithPath:myPathDocs];
NSLog(#"myString %#",myPathDocs);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); // }
size_t pageCount = CGPDFDocumentGetNumberOfPages(pdf);
}
for your Leavies View init function

Multiple Application Support Directories for iPhone Simulator?

I am developing an iPhone app with someone else. The app works fine for me, but he is running into a bug. We think this bug is related to the fact that he is getting multiple Application directories for this same app. In my ~/Library/Application Support/iPhone Simulator/User/Applications, I only have one folder at all times.
He says that he will get 3 or 4 directories when he is only working on this one app. We think this is our problem because our bug has to do with displaying images that are stored in the app's Documents folder. Does anyone know why he is ending up with multiple directories or how to stop it?
Edit:
Here is the code for writing the image to a file:
NSData *image = [NSData dataWithContentsOfURL:[NSURL URLWithString:[currentArticle articleImage]]];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath = [array objectAtIndex:0];
NSFileManager *NSFM = [NSFileManager defaultManager];
BOOL isDir = YES;
if(![NSFM fileExistsAtPath:imagePath isDirectory:&isDir])
if(![NSFM createDirectoryAtPath:imagePath attributes:nil])
NSLog(#"error");
imagePath = [imagePath stringByAppendingFormat:#"/images"];
if(![NSFM fileExistsAtPath:imagePath isDirectory:&isDir])
if(![NSFM createDirectoryAtPath:imagePath attributes:nil])
NSLog(#"error");
imagePath = [imagePath stringByAppendingFormat:#"/%#.jpg", [currentArticle uniqueID]];
[image writeToFile:imagePath atomically:NO];
And here is the code for getting the path when I need the image:
- (NSString *)imagePath
{
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imagePath = [array objectAtIndex:0];
return [imagePath stringByAppendingFormat:#"/images/%#.jpg", [self uniqueID]];
}
The app works great for me, but my partner says that the images don't show up intermittently, and he notices that he gets multiple directories in his Applications folder.
I had this problem (I was saving photos in the apps documents directory) and after every new build the directory get's renamed, so my paths were no longer valid. I cooked up these 2 functions (in my app delegate) that will give me a path for the file I want to save or load from the documents or temp directory. Even if the app directory changes, as long as you only store the file name and not the full path, and then use your helper functions to get the path when you need it later you will be ok. Here's my functions for this:
+ (NSString*)fullPathToFile:(NSString*)file {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:file];
}
+ (NSString*)fullPathToTemporaryFile:(NSString*)file {
return [NSTemporaryDirectory() stringByAppendingPathComponent:file];
}
Works like a charm.