Where should I store an image selected on the iPhone? - iphone

I want to take a picture or select an existing picture from the users existing photos. What is the best way to do this?
Specifically I am concerned where I should to store the image. The storage location should be private to the application. I need to be able to reuse the image as a background every time the application opens. Thanks!

You should use the UIImagePickerController to retrieve images from the library or the camera. You can persist the picture in the App's Documents folder. This folder is private to your app and is writeable.

You can get the path to the documents folder like so
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
And to save a file there.
NSString *filePath = [NSString stringWithFormat:#"%#whatever.jpg",docDirectory];
NSData *toSave = UIImageJPEGRepresentation(image,1.0); //image is a UIImage
[toSave writeToFile:filePath atomically:YES];

Related

Open pdf with other apps

I am displaying a pdf file in an app. I want to show "open with" option on nag bar showing apps installed on iPhone that can open same pdf and if user selects any of the app (for e.g. pdf viewer) then the pdf should get open with pdf viewer app.
How do I do this?
Please help
Thanks in advance.
To open a file in an available application on the device, use the UIDocumentInteractionController class.
A document interaction controller, along with a delegate object, provides in-app support for managing user interactions with files in the local system. For example, an email program might use this class to allow the user to preview attachments and open them in other apps. Use this class to present an appropriate user interface for previewing, opening, copying, or printing a specified file.
There are a lot of questions around it on SO if you get stuck. search results for UIDocumentInteractionController
This code will present the OpenIn interaction you're looking for. It works for iPhone and iPad. On iPad its in a popover. I'm generating the PDF but if you're just using one you have you don't need to writeToFile, just hand in the filePath.
// In your header. MyViewController.h
#interface MyViewController : UIViewController <UIDocumentInteractionControllerDelegate>
{
UIDocumentInteractionController *docController;
}
// In your implementation. MyViewController.m Open Results is hooked up to a button.
- (void)openResults
{
// Generate PDF Data.
NSData *pdfData = [self makePDF];
// Create a filePath for the pdf.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Report.pdf"];
// Save the PDF. UIDocumentInteractionController has to use a physical PDF, not just the data.
[pdfData writeToFile:filePath atomically:YES];
// Open the controller.
docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
docController.delegate = self;
docController.UTI = #"com.adobe.pdf";
[docController presentOpenInMenuFromBarButtonItem:shareButton animated:YES];
}
You can check with Apple Default api of "UIDocumentInteractionController".
Below is url:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html
Hope, it will resolve your issue.
Cheers.

how to store wikipedia document in local database or in application itself by programming?

I am implementing on wikipedia iphone application.In which I have implemented below features.
User can search the wikipedia using keyword.
User can view wikipedia in webview.
Query.
Now I want to store this wikipedia in application or sqlitedabas.
So when net is not available at that time user can view wikipedia from the application or local database.
PLease help me for this query..
Thanks in advance
Well you need to use Core Data to store and manage your info inside a SQLite DB.
Then you need to build a system that lets you store those info inside your DB.
Start here for a Core Data http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/CoreData_ObjC/_index.html
convert webview into image and then save it in database
i think this will help you
CGSize sixzevid=CGSizeMake(1024,1100);
UIGraphicsBeginImageContext(sixzevid);
[webview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(viewImage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathFloder = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%#",#"new.png"]];
NSString *defaultDBPath = [documentsDirectory stringByAppendingPathComponent:pathFloder];
[imageData writeToFile:defaultDBPath atomically:YES];
for more details check the below link
http://www.iphonedevsdk.com/forum/iphone-sdk-development/21451-save-contents-uiwebview-pdf-file.html

how do i remove coredata from iphone

You know how you can Reset the coredata store on an iPhone simulator when you've changed your entity structure?
Do I need to perform a similar process when I've created a new version of my core data store that is different from what I last ran on my iPhone? If so, how, please?
Thanks
Just for convenience, until you code a way to remove the persistent store through your app, you can just delete the app off the phone. (Hold your finger on the home screen until icons get wiggly, then click the x on your app.) Then with your phone connected to your Mac, choose Product > Run in XCode and it will reinstall your app on the phone, but with empty data directories.
For deployment, of course, you need to come up with a way to do it without deleting the app, if you will ever change your data model after deployment (assume you will). Data migration is the best option, but if all else fails delete the persistent store file. It would be preferable to prompt for the user's approval before doing that. If they have important data they can decline and maybe get the old version of your app back to view the data and migrate it by hand, or they can wait until you release version 2.0.1 that fixes your data migration bug.
Here is the routine I use to reset my App content. It erases the store and any other file stored.
- (void) resetContent
{
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];
NSArray *content = [localFileManager contentsOfDirectoryAtURL:rootURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsSubdirectoryDescendants error:NULL];
for (NSURL *itemURL in content) {
[localFileManager removeItemAtURL:itemURL error:NULL];
}
[localFileManager release];
}
If you only want to erase the store, since you know its file name, you can refrain from enumerating the document directory content:
- (void) resetContent
{
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];
NSURL *storeURL = [rootURL URLByAppendingPathComponent:#"myStore.sqlite"];
[localFileManager removeItemAtURL:storeURL error:NULL];
[localFileManager release];
}
But please note that in many cases, its better to migrate your store when you change your model, rather than to delete it.
locate your app in /Users/username/Library/Application Support/iPhone Simulator/4.3.2 (iOS Version may be different) and delete the .sqlite file
You can look at the path that is being sent to the persistentStoreCoordinator on setup, and remove that file. Usually the approach I have taken is that I set up the store to auto migrate, and if that fails I delete the store and attempt one more time to create the persistentStoreCoordinator which will use the now empty path.
Don't forget you may need to repopulate anything stored in the old database.

How do I format and put images in an email from my app?

There have been some older posts about this with very complicated answers. I was wonder if there is a simple way to take an image that is in your project and put it in an email, composed your app but sent through the mail program.
I also don't know how to format it. If I put \n the mail program is never opened.
Here is what does work:
NSString *url = [NSString stringWithString: #"mailto:?&subject=Hello%20There!&body=Really%20Cool.%20Check%20this%20out!"];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
And I would like to add an image and make it look nice, with newlines. Could someone help me out here?
Thanks!
R
You have two options, image as attachment with a mail compose view, or inline as HTML, which means you'd have to upload the app's images somewhere accessible with a permalink or fixed URL. Even if you can form a URL with a local path, the email recipient doesn't receive mail using your app, so it's sandboxed away from the world.
If you generate the pictures in the app it's not easier than a mail compose view, but if uploading them is okay it's dead easy.
I solved it today by uploading optimized and smaller jpg versions of the png images with FTP and linking to it with <b><img src="http://blah.etc.org/myfolder/mypic.jpg" /></b>. The <b> tags were needed to "trick" openURL into not stripping the image back in SDK 3.0, they might not be needed now.
You will want to use MFMailComposeViewController to send an attachment.
Attach an image that is part of your app (in the same directory) to an email like this:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"My Image Is Attached"];
//other mail settings here
//now add your attachment
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *whereToFindFile = [NSString stringWithFormat:#"myImage.png"];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:whereToFindFile];
NSData *imageData1 = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
[picker addAttachmentData:imageData1 mimeType:#"image/png" fileName:#"myImage"];

How to add a ringtone from an application to ringtones of iphone?

I have created an application included a ringtone, but how can i add it to ringtones of iphone?
Use iTunes file sharing in your app and copy the ringtone file to the app's documents directory.
Set "Application supports iTunes file sharing" to YES in your info.plist
Wherever appropriate in your app copy out the file with the code below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"MyRingtone" ofType:#"m4r"];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:filePath];
[[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:#"MyRingtone.m4r"]
contents:mainBundleFile
attributes:nil];
The user can now access the ringtone via itunes and add it to their device's ringtones.
You cannot. Apple doesn't release an API to export/write ringtones to the operating system.