Save iPhone Photo Library Path to a Plist - iphone

I want to save an image path string for a particular photo from the main iphone photo library in a plist file.
So that later I can call the plist image path string in a web view and see the photo.
I know I need the absolute path which is probably a similar path to the documents directory but:
1.) How do I get the photo name of the particular photo?
2.) Do I need to append the absolute path string to the photo name and then write to the plist?
3.) Will the path change for every install of my app - which would make a hard coded absolute path impossible to use?
Any pros want to tackle this conundrum?

If you are building an app targeting lower than iOS4.0, then you may use UIImagePickerController Class. This will let user pick one image from the phone album, which then, you can save in your app's documents directory and save the corresponding link in the info.plist file. The documents directory gets deleted when your app gets uninstalled, and so you lose the path as well.
A better way, with a disadvantage of restricting this feature (and so your app if its a main feature) to only iOS4.0 and above, would be using ALAssetsLibrary. Each photo in the iphone is represented by a unique URL that you can save, and you can refer to a particular image using that url. There is a nice tutorial blog that can help you get started with it.
Now, in your question, you are talking about a particular photo. You can not just know of any particular photo in the library, unless you let the user pick one. This brings me to answer your last question. The URL is constant for that particular image (till the iphone is reset), but unless you have any way of keeping that information between every install of your app (what exactly do mean here?), I don't know how being a unique URL is going to help you here.
EDIT:
Just saw the tag of sdk 4.0 there, please ignore the first case (the <4.0 part).

OK I think I want to use the UIImagePickerController!
Here is my code:
- (void)viewDidLoad {
self.iconPicker = [[UIImagePickerController alloc] init];
self.iconPicker.allowsEditing = YES;
self.iconPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.iconPicker.delegate = self;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
iconImage.image = [info objectForKey:UIImagePickerControllerEditedImage];
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (IBAction)save {
[plist.data setValue:iconImage.image forKey:#"Icon"];
[plist.data writeToFile:plist.file atomically:YES];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
How do I save the image name of the edited image in plist string.
I know I don't want to save the CGImage data or whatever because of the load size.
I think I can pass an xcode variable into HTML through putting all my HTML into string objects and then adding variables to the objects where I need includes...
So I can create a path to the photo directory but I need to know how to get the name of the saved edited image from UIImagePickerController???

Related

Path to picture on iphone

I'm not quite new but still have a few noob questions this is one of them.
I need a path to one of the pictures all ready in my camera roll on my test iphone. How do I get/find this, not programmatically. I want to hard code it for testing for file upload so I can trouble shoot.
so
filePath:#"/private/var/mobile/Applications/14C39895-E73B-400F-8158-F1F5047A826C/tmp/upload.jpg"
should be what:
filePath:#"fill in this spot"
You can't access the photos directly (unless your app wrote them to the docs folder to begin with -- in which case you would know the path). You'll need to implement a UIImagePicker to select the photo from your photo album. The UIImagePicker delegate will then have access to the photo where you can do whatever you want with it(including writing a copy of it to documents). After the photos been selected once and written to docs, you could then access it in documents on later executions of the app.
The UIImagePicker delegate should implement:
-(void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *) info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
Then you can write the image to your apps docs folder in that delegate method, something like this:
http://blog.objectgraph.com/index.php/2010/04/05/download-an-image-and-save-it-as-png-or-jpeg-in-iphone-sdk/
At that point you could always access the photo in your apps docs folder for upload testing... You probably need to implement a UIImagePicker for your app anyway, so instead of looking for your test photo, it may be easier to just select from photo album using UIImagePicker and upload directly (cutting out the whole writing/reading from docs folder).
Hope that helps!

ASIHTTPRequest - Uploading files from the camera roll

I currently need to upload large files from an iDevice to a server API. For this I'm attempting to use the ASIHTTPRequest library as it automatically supports uploading large files in queued chunks (before this I simply created an NSData instance with the bytes for the entire file at once and attached this to the POST message, but this causes the application to crash on larger files due to an excessive amount of RAM usage).
The problem is that when you need to upload files and add a file to the HTTP Post message, this is the syntax you need to use:
[theUploadRequest setFile:#"" forKey:#"videoupload"];
setFile requires a file path in a string format. The problem I'm currently having is that it does not seem like you are allowed to simply take the file path from a file which is not in your applications sandbox? Since I need to upload a file which is not in my application, but outside of it in the standard cameraroll.
I tried to make this quick test to see if I could create an NSData object and fill it with data from a file in the cameraroll, providing a path to it like this:
NSData *testData = [NSData dataWithContentsOfURL:theContent.defaultRepresentation.url];
NSLog(#"THE SIZE OF THE TEST DATA: %i", testData.length);
Note that "theContent" is an instance of an ALAsset, and is a file retrieved from the cameraroll. The result of this is simply a length of 0, which I suppose means you can't simply do that.
Is there any way around this? Or would I have to somehow import the video file into the application's sandbox?
So if I understand correctly, you want to upload stuff straight from the camera roll? Based on the docs I'd say the important piece of code you need is:
NSString *filePath = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
The info dictionary is passed to your media picker's delegate:
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info;
You should then be able to use that filePath in your call to setFile.
Take a look here probably you'll find something useful.
Hope this helps.
EDIT:
For something simpler you can use the bit of code posted in this answer
Implying that you need to copy the file somewhere before uploading it, especially if it is a big one.

AssetsLibrary: Location Services prompt. How did Instagram avoid it?

I am working an app that uses the photos and videos in the AssetsLibrary, and I am just trying to determine once and for all if there is any way around asking the user for permission to access Location data in order to get these assets. I understand that the EXIF data includes GPS information, and that makes enough sense to me.
Note: I have searched through StackOverflow and I have found similar questions, and I am not making this post simply to add one more to the list. I am asking specifically about one (apparent) counterexample.
When using Instagram for the first time, I am able to browse through my photo album, select photos, edit them and share them all without ever being prompted about location services. I am only prompted when I choose to click on the button labeled "Enable Geotagging." Checking the settings tab, it looks like if I don't ever click that button, Instagram doesn't even come up in the Location Services section of my Settings.
My question is, how did Instagram get away with this? Anyone have any ideas? I'd like to figure out if I can mimic their implementation somehow so my users aren't shut out from getting their camera assets if they say no to this prompt.
the explanation is quite simple. Instagram uses the UIImagePickerController. UIImagePickerController works without Location Services enabled, but you don't get EXIF data using this method.
UIImagePickerController can retrieve metadata (incl. GPS) only through the UIImagePickerControllerReferenceURL. UIImagePickerControllerReferenceURL you have to pass then AssetsLibrary methods, which needs again location services enabled.
Cheers,
Hendrik
As holtmann mentioned, reading the UIImagePickerControllerReferenceURL triggers the location services prompt. If you only need the image data, not the metadata, you can get that from the UIImagePickerControllerOriginalImage and UIImagePickerControllerEditedImage keys instead (I'm checking EditedImage first and then checking OriginalImage if EditedImage is null). This doesn't require the use of the assets library and doesn't require location access.
Here's how I'm using this in my app, including saving a local copy of the image for further editing:
- (void)imagePickerController:(UIImagePickerController *)controller didFinishPickingMediaWithInfo:(NSDictionary *)info {
// get the selected photo as a UIImage
UIImage *photo = [info objectForKey:#"UIImagePickerControllerEditedImage"];
if (!photo) {
photo = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
// save the photo to the app's Documents folder
if (photo) {
NSString *extension = #"jpg";
NSString *filename = [NSString stringWithFormat:#"%#.%#", self.defaultTitle, extension]; // self.defaultTitle is defined elsewhere in my app
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:filename];
[UIImageJPEGRepresentation(photo, 0.8) writeToFile:path atomically:YES];
}
}

In the UIImagePickerController, is it possible to get the NSURL value for UIImagePickerControllerMediaURL?

When the user selects an image from the picker controller, I'm calling the delegate:
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
in the dictionary info, the value for key UIImagePickerControllerMediaURL is NULL. Am I missing something here?
I know this question is horribly old, but I did manage to wander across it from a recent google search, so thought I'd throw in what I think is actually the answer to your question which the other answers seem to miss.
You're getting NULL for UIImagePickerControllerMediaURL as it is only returned for the Movie (kUTTypeMovie) media type in a picker. If you're presenting a default picker without specifying additional media types after it's creation, then the docs seem to imply that you won't get it:
"UIImagePickerControllerMediaURL - Specifies the filesystem URL for the movie."
And I can confirm that it isn't returned at least in the quick test that I did in iOS 6 for images (not Movies).
To access the image returned, you actually have direct access to a UIImage object under the keys of UIImagePickerControllerOriginalImage (if editing is not allowed, the default setting) or UIImagePickerControllerEditedImage if editing is allowed in the picker. The original image is always available, even if editing was done in the picker. You can treat these UIImage's just like you would any other UIImage in your program.
You can get an NSURL path to the object, but it isn't quite what you would expect. And I can't seem to access the data in the file either.
- (void)imagePickerController:(UIImagePickerController *)imagepicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// dismiss the image picker modal
[[imagepicker parentViewController] dismissModalViewControllerAnimated:YES];
// grab the image url
NSURL *imageURL = [info valueForKey: UIImagePickerControllerReferenceURL];
// get the data from the url
NSData *data = [NSData dataWithContentsOfURL:imageURL];
// data is null at this point
}
The NSURL looks something like the following for a jpeg file.
assets-library://asset/asset.JPG?id=1000000246&ext=JPG
Yes the Meta data is not populated in the callback (some were also expecting the geotag / position of the place where the picture was taken , but this is not the case).
The fact that you don't get the NSURL sounds logical from Apple perspective : they will not give you the path to the real file (like file:// ....) because this is something that is considered as part of the implementation, not part of the public API : from the client perspective it looks like if apple doesn't want you to known where the picture is store physically on the File system.
Running iOS 3.2.1 on an iPad, it seems that my app is able to get the file URL via UIImagePickerControllerMediaURL for image files, even though the documentation seems to say that it only works for movie files.
The following link helped me. display image from URL retrieved from ALAsset in iPhone
The UIImagePickerControllerReferenceURL returns an asset-library url. You need to use an ALAssetLibrary object to access the image. The link describes how to do this.

Retrieving an iPhone photo saved to the photo library

I am saving images to the photo library and would like to retrieve them dynamically to display in future launches of my app. I use the WriteToSavedPhotosAlbum function, as below, but do not get any info to access the saved photo programmatically.
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
Once saved, is there a way to retrieve the file name/id and load an image from the photo library at a later time?
You just need to save them for yourself.
Possibly in your app's documents or cache folder.
As a side note, when you say: "display at launch", if you mean to replace the Default.png, that won't work.
The launch image must be static and included in your app bundle.
If not, disregard.
You can't, unless the user picks the photo using the UIImagePickerController.
Check this related question
try this code segment make the necessay changes
UIImagePickerController *picker=[[UIImagePickerController alloc] init];
picker.delegate=pickerDelegate;
picker.sourcetype=UIImagepickerControllerSourceTypePhtoLibrary;