Application crashing in the imagePickerController:didFinishPickingMediaWithInfo method inside UIImagePickerController - iphone

I am a beginner in iOS development, I tried to make an application to select an image from the UIImagePickerController which is displayed inside a UIImageView object. The problem I am facing is inside this method
-(void)imagePickerController:(UIImagePickerController*)Picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSLog(#"Inside dismiss modal view delegate 2");
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
selectedImage.image = [image retain];
NSLog(#"Inside the did pickerController method");
}
When I select an image from the UIImagePickerController, the image gets selected but the application crashes. I tried debugging, and the first NSLog statement gets displayed on the console, but the 2nd one does not get displayed.
I am trying to load an image from the Photos library inside the simulator. I am using XCode 3.2.5 and the simulator is 4.2. This might men the error is there in between the two NSLog statements. But, I have tried my best to find out the error, but without any success. Can you please help me out.

NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
imgPicture.image = [[UIImage alloc] initWithData:dataImage];
[picker dismissModalViewControllerAnimated:YES];
Put this code in method

Related

Select Multiple Images from Photo Library

I am going to ask that one question that perhaps has been already asked a million times.
I am making an app for iPad and want to give users the ability to multi-select images from their photo-library. I already have a working code for user to select one image at a time. (not what I need)
I have already downloaded and looked into ELC image picker sample code but that code is not compatible with iOS 5 or Xcode 4. i.e. it has ARC and compile problems left and right, its using release and dealloc all over the place.
I am just frustrated that apple hasn't already created a built in-api for us developers for this most commonly requested functionality in most of our iPhone/ipad apps. (not one but multi-select pics)
Is there any other sample code available? Trust me, I have been googling for a while.
Ok, I have this figured out. The problem with Assets Library is that it gives you all the GEO data of the image. What that means for your users using your app is that they will get a prompt saying that your app is trying to access their location. Infact all you are trying to do is let them choose multiple images from their photo-album. Most users will be turned off thinking its a piracy issue. The best approach is to use apples api of imagePickerController. I know it lets you choose one pic at a time but if you add the following code, it will let you choose multiple pictures.
The way I am doing is let the users keep selecting pictures they want, keep saving those files in the app documents directory, till they hit the done button. See here my sample code and hopefully it will save you the pain of going through Assets Library
-(IBAction)selectExitingPicture
{
//Specially for fing iPAD
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popoverController presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 300.0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
//Done button on top
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
//NSLog(#"Inside navigationController ...");
if (!doneButton)
{
doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone
target:self action:#selector(saveImagesDone:)];
}
viewController.navigationItem.rightBarButtonItem = doneButton;
}
- (IBAction)saveImagesDone:(id)sender
{
//NSLog(#"saveImagesDone ...");
[popoverController dismissPopoverAnimated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
//DONT DISMISS
//[picker dismissModalViewControllerAnimated:YES];
//[popoverController dismissPopoverAnimated:YES];
IMAGE_COUNTER = IMAGE_COUNTER + 1;
imageView.image = image;
// Get the data for the image
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
// Give a name to the file
NSString* incrementedImgStr = [NSString stringWithFormat: #"UserCustomPotraitPic%d.jpg", IMAGE_COUNTER];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr];
// and then we write it out
[imageData writeToFile:fullPathToFile2 atomically:NO];
}
//Now use this code to get to user selected pictures. Call it from wherever you want in your code
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
NSString* documentsPath = [paths objectAtIndex:0];
NSString* dataFile = [documentsPath stringByAppendingPathComponent:#"UserCustomPotraitPic1.jpg"];
NSData *potraitImgData = [NSData dataWithContentsOfFile:dataFile];
backgroundImagePotrait = [UIImage imageWithData:potraitImgData];
Apple has provided api for this. It is called ALAssetsLibrary.
Using this you can select multiple images/ videos and other operations that you do using photo application on iOS device.
As in documentation Apple says:
Assets Library Framework
Introduced in iOS 4.0, the Assets Library framework
(AssetsLibrary.framework) provides a query-based interface for
retrieving photos and videos from the user’s device. Using this
framework, you can access the same assets that are normally managed by
the Photos application, including items in the user’s saved photos
album and any photos and videos that were imported onto the device.
You can also save new photos and videos back to the user’s saved
photos album.
Here are few links where you can learn more. Now to use it you can search for ALAssetsLibrary.
Assets Library Reference
http://www.fiveminutes.eu/accessing-photo-library-using-assets-library-framework-on-iphone/
Starting with iOS 14, multiple image selection is now supported in the new Photos picker. Here is sample code from Apple: Selecting Photos and Videos in iOS.
I use ALAssetsLibrary and rolled my own UI. The problem with UIImagePickerController is that it says you are supposed to dismiss the view controller in the didFinishPickingMediaWithInfo callback, so hacking multiple selection by not dismissing may run into problems. I know I did when I first tried it. I can't recall exactly what went wrong, but there were cases where UIImagePickerController simply stopped working if I didn't dismiss it like the docs say.

Image Saving to Albums working fine in Simulator but gives EXC_BAD_EXCESS on device

I want to save an image into albums.
I am doing it using UIImageWriteToSavedPhotosAlbum API.
Its working perfectly fine on Simulator, but crashes on Device giving EXC_BAD_EXCESS.
I have enables NSZombi.. argument, but it shows no log of any deallocated memory reused.
- (IBAction)saveImageOnHDButtonPressed:(id)sender{
NSArray *subviews = [scroll subviews];
UIImage *img = [[subviews objectAtIndex:0] image];
UIImageWriteToSavedPhotosAlbum(img, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
Please help..
Thanks
Try retaining the image before you save it.

iPhone camera delay after taking picture in flash mode

iPhone camera delayed after taking picture in flash mode, I used UIImagePicker controller and used takePicture for capturing the picture, it is working well in all modes except if the flash mode is on. when it is on it is taking some time to process the image. why is this happening. Can anybody help me in this. my code is below.
to take picture i used
[picker takePicture];
after user took the picture...
imagePickerController:didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModelviewControllerAnimated:YES];
}
I find it takes a while to process the picture under any sitaution. In my didFinishPickingMediaWithInfo I load up a view over the top of my main view to tell the user I'm processing the image.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *picture = [info valueForKey:UIImagePickerControllerOriginalImage];
[self dismissModalViewControllerAnimated:YES];
[self performSelectorInBackground:#selector(processImage:) withObject:picture];
_activityView = [[PSActivityView alloc] initWithFrame:CGRectZero];
_activityView.text = NSLocalizedString(#"Processing...", nil);
[self.view addSubview:_activityView];
[self.view bringSubviewToFront:_activityView];
}
The PSActivityView class sizes itself in the layoutSubViews method and the processImage method deals with the image and dismisses the _activityView when it's finished.
Seems to keep users happy with my app.

How to reload image in UIButton in applicationDidBecomeActive

I have a MenuViewController that loads when the app loads up; it is the root view of a UINavigationController.
Within the view I have a UIButton with an image that loads from a URL, as well as a label (its a picture indicating the weather and the current temp.).
Everything works fine, but if I am using the app on an iPhone 4, with multi-tasking, when the home button is pressed and then the app is reopened, I need the UIButton image and temp. label to reload.
I have tried calling this in my AppDelegate under applicationDidBecomeActive:
[self parseWeatherXML]; //re-parse weather data
[menuViewController reloadWeatherData]; //loads the image/label from the XML
with no luck.
I have also tried releasing menuViewController in applicationWillResign, then reallocating menuViewController in applicationDidBecomeActive so the viewDidLoad method gets called again, both ways just end up crashing my app.
Any help appreciated!
EDIT
Heres my method that gets called with the notification:
- (void)reloadWeather
{
[self parseWeatherXML];
UIImage *img = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:iconURL]]];
if (img != nil)
{
NSLog(#"setting image");
[weatherButton setImage:img forState:normal];
img = nil;
}
currentTemp = [currentTemp stringByAppendingString:#"°F"];
[tempLabel setText:currentTemp];
tempLabel.adjustsFontSizeToFitWidth = YES;
if([self isIPad])
{
tempLabel.textAlignment = UITextAlignmentRight;
[tempLabel setFont: [UIFont systemFontOfSize: 45.0]];
}
currentTemp = nil;
}
I would have your MenuViewController become an observer to the UIApplicationDidBecomeActiveNotification and perform the parseWeatherXML data. I would think you would be refreshing the data from the URL, so you would need to wait for that data to come back again. So, I would follow the same logic that you are doing when you receive that notification.
UIApplicationDidBecomeActiveNotification
Posted when the application becomes
active. An application is active when
it is receiving events. An active
application can be said to have focus.
It gains focus after being launched,
loses focus when an overlay window
pops up or when the device is locked,
and gains focus when the device is
unlocked.
Availability
Available in iOS 2.0 and later.
Declared In
UIApplication.h
Have you tried saving the image and temp using a plist or something, during the applicationWillResign, and then during applicationDidBecomeActive, you can reload the image and temp from the saved file/label. Just another option to explore.

iPhone 4.0 Simulator: didFinishPickingMediaWithInfo is missing UIImagePickerControllerOriginalImage?

I've got a simple UIImagePickerController which tries to grab the original selected image:
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
if(defaultpicker == nil){
defaultpicker = [[UIImagePickerController alloc] init];
}
defaultpicker.delegate = self;
defaultpicker.allowsEditing = NO;
defaultpicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:defaultpicker animated:YES];
}
Upon selecting:
- (void)imagePickerController:(UIImagePickerController *)imagepicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[imagepicker dismissModalViewControllerAnimated:YES];
NSString* key = nil;
for(key in info){
NSLog(#"Info: %#", key);
}
UIImage *theImage = (UIImage *)[info objectForKey: UIImagePickerControllerOriginalImage];
I'm using 4.0 as the base SDK, and targeting 3.1.3 at the moment. Running the 4.0 simulator, the info collection only contains:
2010-07-07 16:19:33.414 ******[516:307] Info: UIImagePickerControllerMediaType
On the device, or running in the iPad 3.2 simulator I get:
2010-07-07 16:19:33.405 ****[516:307] Info: UIImagePickerControllerOriginalImage
2010-07-07 16:19:33.414 ****[516:307] Info: UIImagePickerControllerMediaType
Am I missing something? This was working fine before I updated to SDK 4.0. I have no warning etc.
Obviously, without the original image in the simulator, I can't show or do anything with the selected image as I have no idea what it is.
Im running on a device running 4.0 (a 3GS) and getitng the same problem. It worked fine before as well. So it is not a simulator error. However for me this only seems to be happening if it is a user created photo album. if selected from the camera roll or taken with the camera it works fine :/.
If i find a solution (working on it now) will post.
Update: It appears to be a bug with apple, since as we both stated, the info dictionary that is returned only contains the key-value pair of the media type even though this value is "public.image" it does not return that image in the UIImagePickerControllerOriginalImage key as it should. I have submitted a bug report Bug ID# 8176175.
I was getting this as well, but if you choose this you will get them both(for now it looks a like a workaround):
self.imagePicker.allowsEditing = YES;