In my app I am taking a picture and showing it in an image view.
Now I need the path of that image so I can upload it to a server.
How do I get that path? I try importing ALAsset framework but there is no framework called that my x code (4.2) iOS 5.0
NSData *data = UIImagePNGRepresentation(imagePic.image);
NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:#"upload.jpg"];
[data writeToFile:file atomically:YES];
[[EPUploader alloc] initWithURL:[NSURL URLWithString:#"myWebSite"]
filePath:#"file"
delegate:self
doneSelector:#selector(onUploadDone:)
errorSelector:#selector(onUploadError:)];
This is what I am currently trying.
Also what would be a hard coded path to a photo in an iphone camera roll? "iphone/pictures/myPic" ?? so I can try the upload separate from finding the path.
Please and Thank you
EDIT:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
image2 = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image2, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
imagePic.image = image2;
[picker release];
}
An image view has no path, it's in memory. You'll have to save the path you get when you return from taking the image. In the delegate method imagePickerController:didFinishPickingMediaWithInfo: you can get the NSURL in UIImagePickerControllerMediaURL which points to the image on disk:
NSURL *imageURL = [info valueForKey:UIImagePickerControllerMediaURL];
Related
I'm interested in uploading a file (image) from an iPhone library/camera roll to a remote web server. I already have a script working that will upload any file from the phone to a web server. However, I assume that to upload an image from the iPhone, I need the PATH to said image. Is there any way this can be done, once the user picks said image from the camera roll? I.e., how do I get the file path of an image selected in the camera roll?
I have tried to no avail.
Thanks!
You will want to look at the ALAssetsLibrary functions - these let you access photos and videos that are stored in your Photos and Videos libraries on your iOS device.
Specifically, something like:
ALAssetsLibrary *assets = [[ALAssetsLibrary alloc] init];
[assets enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
//the ALAsset should be one of your photos - stick it in an array and after this runs, use it however you need
}
}
failureBlock:^(NSError *error) {
//something went wrong, you can't access the photo gallery
}
];
EDIT
If you are using the UIImagePickerController rather than a purely programatical approach, this simplifies it greatly:
In:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
//you can use UIImagePickerControllerOriginalImage for the original image
//Now, save the image to your apps temp folder,
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:#"upload-image.tmp"];
NSData *imageData = UIImagePNGRepresentation(img);
//you can also use UIImageJPEGRepresentation(img,1); for jpegs
[imageData writeToFile:path atomically:YES];
//now call your method
[someClass uploadMyImageToTheWebFromPath:path];
}
I'm trying to save Images the user takes with the camera
1) If I use the UIImageWriteToSavedPhotosAlbum I can't seem to assign the fileName that I want. How, can you choose the file name?
The nice thing about using this option is
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
Then gives a thumbnail gallery of Photo Library directory.
2) which leads me to my next question Can
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; be used to get images from your own personal directory ?
3) Is there anyway to programmatically create sub folders within the Photo Library
4) Lastly,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
if (![[NSFileManager defaultManager] fileExistsAtPath:[paths objectAtIndex:0] isDirectory:&isDir]) {
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:[paths objectAtIndex:0 withIntermediateDirectories:YES attributes:nil error:&error];
}
Checking to see if the NSPicturesDirectory exists so I can write to it
I keep getting a Cocoa error 513 not permitted
Thanks in advance
You can't assign a file name to photo library images. ios assign a file name called asset url to the images that are saved to the photo library, we can't change that.
If you need to save the images to photo library you can use ALAssetsLibrary
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation) [image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error)
{
// Eror
}
else
{
// Success
}
}];
[library release];
For more information check:
How to save picture to iPhone photo library?
Save image in UIImageView to iPad Photos Library
1.)
To save a filename of your choice, you need to save the image to your documents directory. This can easily be done like so:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSString *current = [NSString stringWithFormat:#"%#/%#",PHOTODATA_PATH,currentItem];
UIGraphicsBeginImageContext(CGSizeMake(160,160));
[image drawInRect:CGRectMake(0,0,160,160)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * thumbSize = UIImageJPEGRepresentation(scaledImage, 1.0);
[thumbSize writeToFile:[NSString stringWithFormat:#"%#_thumb.jpg",current] atomically:YES];
NSData * fullSize = UIImageJPEGRepresentation(image, 1.0);
[fullSize writeToFile:[NSString stringWithFormat:#"%#.jpg",current] atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
Of course this code may need to be edited to fit your exact situation but gives the basic understanding and way to save a photo to file using NSData.
2.)
Yes, UIImagePickerControllerSourceTypePhotoLibrary will access your Photo Library on your device. Any photos saved or taken from the camera roll will be accessible using this.
3.)
No, you cannot create subfolders in the Photo Library using your application. This can be done using iPhoto or iTunes or the like.
4.)
You only have access to the Documents and Library paths contained within your sandboxed environment. The only way you can save a photo to the Photo Library is by using the appropriate public methods. There is no need to check if the directory exists, the OS will take care of all of the "behind the scenes" tasks it needs to in order to manage the Photo Library.
i will like to pick an images from UIImagepicker, there are PNG and JPG format in camera roll.
and i need to convert it into NSData. However i need to know if this images is UIImageJPEGRepresentation or UIImagePNGRepresentation so i could convert it.
UIImage *orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
NSData *orgData = UIImagePNGRepresentation(orginalImage);
You shouldn't know or care what the internal representation of images in the camera roll is. The methods you mentioned UIImageJPEGRepresentation and UIImagePNGRepresentation return you representations of the camera roll image. It's up to you to pick which representation you want to use.
To summarize:
NSData * pngData = UIImagePNGRepresentation(originalImage);
Will return you the image representation in an NSData object, with the format PNG.
When the delegate method imagePickerController:didFinishPickingMediaWithInfo: for UIImagePickerController is called, you get the asset URL for the particular photo picked.
[info valueForKey:UIImagePickerControllerReferenceURL]
Now this URL can be used to access the asset in the ALAssetsLibrary. Then you would need a ALAssetRepresentation of that accessed asset. From this ALAssetRepresentation we can get the UTI for that image (http://developer.apple.com/library/ios/#DOCUMENTATION/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html)
Maybe the code would make it a bit clearer :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if (!(picker.sourceType == UIImagePickerControllerSourceTypeCamera)) {
NSLog(#"User picked image from photo library");
ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:[info valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *repr = [asset defaultRepresentation];
if ([[repr UTI] isEqualToString:#"public.png"]) {
NSLog(#"This image is a PNG image in Photo Library");
} else if ([[repr UTI] isEqualToString:#"public.jpeg"]) {
NSLog(#"This image is a JPEG image in Photo Library");
}
} failureBlock:^(NSError *error) {
NSLog(#"Error getting asset! %#", error);
}];
}
}
As the UTI explains, this should be a sure shot answer to how the image is stored in the photo library.
in Swift 2.2
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if (!(picker.sourceType == UIImagePickerControllerSourceType.Camera)) {
let assetPath = info[UIImagePickerControllerReferenceURL] as! NSURL
if assetPath.absoluteString.hasSuffix("JPG") {
} else {
}
I have an application, in which the user will select an image from a UIImagePickerView.
After selecting an image from it, I want to save it in my application.
How can this be achieved?
Thanks in advance for helping me.
Assuming you're using SDK 3.0, here is some code to save the image into your application's documents folder:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Dismiss the picker
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
// Get the image from the result
UIImage* image = [info valueForKey:#"UIImagePickerControllerOriginalImage"];
// Get the data for the image as a PNG
NSData* imageData = UIImagePNGRepresentation(image);
// Give a name to the file
NSString* imageName = "MyImage.png";
// Now, we have to find the documents directory so we can save it
// Note that you might want to save it elsewhere, like the cache directory,
// or something similar.
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// and then we write it out
[imageData writeToFile:fullPathToFile atomically:NO];
return;
}
I would say something like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
self.resumePreviousSettingAfterEditing = true;
[self.topImageView setImage:image];
[cubeModel setImage:image forFace:[cubeModel.validFaces objectAtIndex:selectedRowInFacePicker]];
[self dismissImagePickerAnimated:true];
}
You register an event in your controller to handle the image selection. In that event handler, call a method somewhere, say in your model to set the new image. That function would look something like this:
(void)saveImage:(UIImage *)image withName:(NSString *)imageName {
// get the image path
NSString *filename = [self determineImagePath:imageName];
// make sure the file is removed if it exists
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filename]) {
if(NO == [fileManager removeItemAtPath:filename error:NULL]) {
}
}
// Now, save the image to a file.
if(NO == [UIImagePNGRepresentation(image) writeToFile:filename atomically:YES]) {
[NSException raise:#"Image Save Failed" format:#"Unable to store image %s", filename];
}
}
When you want to load the image again, you would so something like:
- (UIImage *)loadImage:(NSString *)imageName {
NSString *filename = [self determineImagePath:imageName];
NSFileManager *fileManager = [NSFileManager defaultManager];
self.currentImage = nil;
if([fileManager fileExistsAtPath:filename]) {
NSData *imageData = [[NSData alloc] initWithContentsOfFile:filename];
UIImage *image = [UIImage imageWithData:imageData];
self.currentImage = image;
}
return self.currentImage;
}
And don't get me started on transforming which is way harder than it should be.
Enjoy,
Jacob
One thing you will need to address when saving images returned by UIImagePickerVIewController is that writing the data to disk will almost always be unacceptably slow. Your UI will hang while the writing is occurring. So, you should always execute these types of operations in an asynchronous queue. Even if the performance seems good enough for your application when testing, you should still do it an asynch queue -- you never know what other processes the device might have going on which might slow the save down once your app is in the hands of users.
Newer versions of iOS make saving photos asynchronously really, really easy using Grand Central Dispatch (GCD). The steps are:
Create an NSBlockOperation which saves the image
In the block operation's completion block, read the image from disk & display it (the only caveat here is that you must use the main queue to display the image: all UI operations must occur on the main thread).
Add the block operation to an operation queue and watch it go!
That's it. And here's the code:
// Grab the image
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
// Create a block operation with our saves
NSBlockOperation* saveOp = [NSBlockOperation blockOperationWithBlock: ^{
[UIImagePNGRepresentation(image) writeToFile:file atomically:YES];
}];
// Use the completion block to update our UI from the main queue
[saveOp setCompletionBlock:^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
UIImage *image = [UIImage imageWithContentsOfFile:file];
// TODO: Assign image to imageview
}];
}];
// Kick off the operation, sit back, and relax. Go answer some stackoverflow
// questions or something.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:saveOp];
Once you are comfortable with this code pattern, you will find yourself using it a lot. It's incredibly useful when generating large datasets, long operations on load, etc. Essentially, any operation that makes your UI laggy in the least is a good candidate for this code. Just remember, you can't do anything to the UI while you aren't in the main queue and everything else is cake.
I have seen this question pop up a few times but the authors see to be satisfied with the wrong answer, so I will ask again.
When picking a movie out of the user's gallery, I am given a MOV in the tmp directory but I am not given the thumbnail for the movie now in the 3.1+ sdk. In sdk 3.0, you get a jpg in the tmp folder but this behavior has stopped.
Is there an answer to get the thumb besides ffmpeg? Anything supported by the sdk?
I get a thumb when they shoot a video directly.
This answer is wrong: iphone sdk > 3.0 . Video Thumbnail?
Found the answer, but I had to wait until iOS4 (the feature came out in 3.2)
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType compare:(NSString*)kUTTypeMovie] == NSOrderedSame) {
// deal with the movie
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
NSURL *mediaUrl = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(#"media Url = %#, path %#", mediaUrl, [mediaUrl path]);
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaUrl];
UIImage *thumbnail = [[moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame] retain];
[moviePlayer release];
NSLog(#"thumbnail = %#", thumbnail);
[thumbnail release];
}
}