Exception in iPhone app : Modal transition is already in progress - iphone

I have what I believe is a fairly simple application at the moment based on a few tutorials cobbled together. I'm using XCode 3.2.3 in OSX 10.6.4. It started as a standard iPhone "Window Based Application". Using interface builder I have added a Tab Bar Controller using the O'Reilly video tutorial here:
http://broadcast.oreilly.com/2009/06/tab-bars-and-navigation-bars-t.html
In the first Tab I have a standard UIView with two buttons. Both call the same function to display a UIImagePickerController:
-(IBAction) btnPhotoClicked:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if((UIButton *)sender == btnChoosePhoto)
{
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
I am running the code inside an emulator so only ever click the button called Choose Photo. When the dialogue is released with a photo chosen this function runs:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *mediaUrl;
mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
if (mediaUrl == nil)
{
imagePuzzle = (UIImage *) [info valueForKey:UIImagePickerControllerEditedImage];
if(imagePuzzle == nil)
{
//--- Original Image was selected ---
imagePuzzle = (UIImage *) [info valueForKey:UIImagePickerControllerOriginalImage];
}
else {
//--- Get the edited image ---
//--- If it was successful the above valueForKey:UIImagePickerControllerEditedImage
//--- would have assigned it already.
}
}
else {
//--- Muppet selected a video
}
// Animate the picker window going away
[picker dismissModalViewControllerAnimated:YES];
ImageViewController *imageViewController = [[ImageViewController alloc] init];
imageViewController.delegate = self;
[self presentModalViewController:imageViewController animated:YES];
[imageViewController release];
}
This is where my problem lies. I've tried many different hacks and iterations but the above code is the simplest to present the problem. When the imageViewController is displayed as a modal dialogue the following exception is thrown:
2010-07-09 15:29:29.667 Golovomka[15183:207] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'Attempting to begin a modal
transition from <NewViewController: 0x5915f80> to <ImageViewController: 0x594a350>
while a transition is already in progress. Wait for viewDidAppear/viewDidDisappear
to know the current transition has completed'
How do I cure this? I have tried delays and other tricks but do not really understand how I'm supposed to use viewDidAppear or viewDidDisappear to help me. Also of note is that a very basic application with one view loading the picker then displaying another view with the image in does not produce the error. Any help gratefully received.

To address the specific issue described here, you could add the viewDidAppear method in your class:
-(void)viewDidAppear:(BOOL)animated
{
if (/*just visited ImagePicker*/)
{
ImageViewController *imageViewController = [[ImageViewController alloc] init];
imageViewController.delegate = self;
[self presentModalViewController:imageViewController animated:YES];
[imageViewController release];
}
}
Remove those lines from below your call:
[picker dismissModalViewControllerAnimated:YES];
So, whenever your class self appears (is displayed), it will call viewDidAppear... Since this most likely isn't really what you want all the time, you could add some variables to set/clear that defines whether or not to immediately present the imageViewController when self is displayed. Something like "If coming from image picker, show the imageViewController, otherwise do nothing".
That said, imho, pushing modal views is should generally be done in response to a user action and I would maybe rethink the user experience here - e.g. add a subview instead of pushing a modal view which you could do where your currently have the code - but if you're just playing around with some tutorials that should solve the NSInternalInconsistencyException. :) Cheers!

In iOS 5.0 and above you can use
[self dismissViewControllerAnimated:YES completion:^{
//present another modal view controller here
}];

I ran into this issue quite a few times. I recently started using this simple fix:
When I am going to present a new modal view controller immediately after dismissing another modal view controller, I simply dismiss the first one with argument NO in dismissModalViewControllerAnimated:.
Since the second view is presented with an animation, you hardly notice that the first one goes away fast. And you never get the transitions conflict.

I was having the same problem when i wanted to present an MFMailComposeViewController immediately after dismissing the UIImagePickerController. Heres what i did:
I removed the [imagePicker release]; statement from where i was presenting the image picker and put it in didFinishPickingMedia callback.
I used [self performSelector:#selector(presentMailComposer:) withObject:image afterDelay:1.0f];
Here's my code:
Displaying Image Picker
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
NSArray *media = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
if ([media containsObject:(NSString*)kUTTypeImage] == YES) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
//[picker release];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unavailable!"
message:#"Could not open the Photo Library."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
Image Picker Delegate Callback - didFinishPickingMedia
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
UIImage *photoTaken = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Save Photo to library only if it wasnt already saved i.e. its just been taken
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(photoTaken, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//Pull up MFMailComposeView Controller
[self performSelector:#selector(composeMailWithPhoto:) withObject:photoTaken afterDelay:1.0f];
}
[picker dismissModalViewControllerAnimated:YES];
[picker release];
Display Mail Composer View
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
// Fill out the email fields and Attach photograph to mail
static NSString *imageType = #"image/jpeg";
NSString *imageName = [NSString stringWithString:#"MyCoffeeCup.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[mailPicker addAttachmentData:imageData mimeType:imageType fileName:imageName];
[mailPicker setToRecipients:[NSArray arrayWithObject:#"hello#xische.com"]];
[self presentModalViewController:mailPicker animated:YES];
//[self.navigationController pushViewController:mailPicker animated:YES];
[mailPicker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unavailable!"
message:#"This device cannot send emails."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}

Related

MFMailComposeViewController ModalView crashes the iOS5 application

I have an iOS TabBar Application with tabbarcontroller and navigationcontroller.
In my detail view wich is pushed from my first tab tableviewcontroller i have sharing navigationItem.rightBarButtonItem with email sharing.
I have the following code for this:
- (void)share
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:#"Send" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Email",nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[popupQuery showInView:self.view];
[popupQuery release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
if ([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[picker setMailComposeDelegate:self];
[picker setSubject:#"New theme"];
NSString *emailBody = #"Hi there";
[picker setMessageBody:emailBody isHTML:NO];
[self resignFirstResponder];
picker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
picker.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentModalViewController:picker animated:NO];
[picker release];
}
else{
}
}
}
The app shows me the composing view but when i'm trying to do something with this view (e.g. to pick up the address or to spell something) - app crashes with SIGTRAP.
The app crashes only in iOS5, iOS5.1. In iOS4.2.1 everything works perfect.
What's the problem? Any ideas?
Per the docs, I'd suggest calling [MFMailComposeViewController canSendMail] class method before creating MFMailComposeViewController. I also generally don't have that [self resignFirstResponder] line. I gather you're crashing before your mailComposeController:didFinishWithResult:error method is invoked?
Thank you guys for your help and your time.
It was absolutely insane bug. Project has a cyrillic name. I just renamed it to latin name and now everything works fine. My fault :( Thanks Evgeniy Shurakov for the help.

iOS5 problems with old applications

I need advice on what to do.
I have several applications on App store and they are all tested and fully functional for all previous iOS's. But now when I update my device to iOS5 some of them began to crash unexpectedly on alert view item that ask for connecting to internet and show map with current location.
I am not that experienced in development so need some advice what to do? I supposed that with final version of iOS5 these things will be fixed by itself or not.
Thanks.
OK I have torned zombies and found method that makes problems.
- (void) alertView: (UIAlertView*) alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(#" Button PRESSED: %d", buttonIndex);
[alertView release];
if (buttonIndex == 1) {
BOOL noConnectionAvailable = NO;
BOOL hasParentalLimit = NO;
switch (lastSelectedItem.itemType) {
case RestaurantItemTypeAddress : {
if ([NetworkHelper connectedToNetwork] == YES) {
AddressController *mapController = [[AddressController alloc] initWithNibName:#"AddressController" bundle:nil restaurant:restaurant];
mapController.title = restaurant.res_title;
[self.navigationController pushViewController:mapController animated:YES];
[mapController release];
} else
noConnectionAvailable = YES;
break;
}
case RestaurantItemTypeReservationEmail : {
if ([NetworkHelper connectedToNetwork] == YES) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Reservation"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:lastSelectedItem.itemTextContent];
[picker setToRecipients:toRecipients];
// Fill out the email body text
NSString *emailBody = #"";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
[self.tableView deselectRowAtIndexPath:lastSelectedIndexPath animated:YES];
} else
noConnectionAvailable = YES;
break;
}
case RestaurantItemTypeReservationForm : {
if ([NetworkHelper connectedToNetwork] == YES) {
if ([NetworkHelper canOpenUrl:lastSelectedItem.itemTextContent]) {
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil urlStr:lastSelectedItem.itemTextContent];
wvc.title = restaurant.res_title;
[self.navigationController pushViewController:wvc animated:YES];
[wvc release];
} else hasParentalLimit = YES;
} else
noConnectionAvailable = YES;
break;
}
case RestaurantItemTypeWeb : {
if ([NetworkHelper connectedToNetwork] == YES) {
if ([NetworkHelper canOpenUrl:lastSelectedItem.itemTextContent]) {
WebViewController *wvc = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil urlStr:lastSelectedItem.itemText];
wvc.title = restaurant.res_title;
[self.navigationController pushViewController:wvc animated:YES];
[wvc release];
} else hasParentalLimit = YES;
} else
noConnectionAvailable = YES;
break;
}
}
if (noConnectionAvailable == YES) {
UIAlertView* newAlert = [[UIAlertView alloc] initWithTitle:#"Jesolo Official Guide"
message:#"Nessuna connessione disponibile."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[newAlert show];
}
if (hasParentalLimit == YES) {
UIAlertView* newAlert = [[UIAlertView alloc] initWithTitle:#"Jesolo Official Guide"
message:#"Navigazione su Web non consentita."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[newAlert show];
}
}
[self.tableView deselectRowAtIndexPath:lastSelectedIndexPath animated:NO];
}
The log is different on iOS4 and iOS5.
On iOS5 it said:
2011-11-04 16:26:28.550 Jesolo-EN[5693:207] Button PRESSED: 1
2011-11-04 16:26:28.776 Jesolo-EN[5693:207] *** -[NSIndexPath isEqual:]: message sent to deallocated instance 0xe6b6fc0
sharedlibrary apply-load-rules all
Current language: auto; currently objective-c
warning: Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame.
(gdb)
and on iOS4 said:
2011-11-04 16:28:08.087 Jesolo-EN[5859:207] Button PRESSED: 1
2011-11-04 16:28:08.162 Jesolo-EN[5859:207] *** -[UIAlertView release]: message sent to deallocated instance 0x78c4940
sharedlibrary apply-load-rules all
Current language: auto; currently objective-c
(gdb)
I understand that i release data before i use them but when I run app without enabled zombies it work fine on iOS4 and on iOS5 it run one time and then 10 times crash.
I am finding that references to ivars that previously worked are now needing to be referenced as self.ivar. Perhaps your reference to managedObjectContext should instead be self.managedObjectContext?
I am not very much sure about this but you can try this also..
Click on your **project**->go to **info**-> go to **build** section-> in that go to **Deployment** section->then **IOS deployment** section choose the **deployment target to IOS 5** or latest and save and run...
Hope it may help you... :)
OK I manage to find solution.
Somehow iOS5 detect some release of objects that doest'n exist in iOS4 and I tried to find them but gave up.
Instead of that I turn on ARC and comment all release and dealloc and my app is doing really fine. I get impression that now it works even faster. Hope this will help somebody...

IOS development, using UIImagePickerController and pressing use make the app get stuck (not crash)

Can any one help me with this problem?
As in my last question, I am using a tabBarController with 3 tab items. The 3rd tab is has a uiViewController with a UIImagePickerController in it(a camera).
now every things is working except from one thing. When take an image with the camera and pressing "use" i am getting the alert the the image was save and i can see it in the photo album (if i close the app and look at it) but the app get stuck at this poing and i can not do anything any more. I can see the image on the screen and the "use" and "retake" buttons are not usable. just stuck like that.
Can any one see what am i doing wrong over here?
ps. In all of the examples and tutorials i found there is a release of the picker in the cancel...(also in my code). The picker in my case is a property of the view controller (imgPicker) and i release it as always in the dealloc method, Is that write or wrong? should i live it like that or am i doing a bad memory thing over here (I am not getting any "bad memory error" over here but it might be my mistake...)?
I load the UIImagePicker in the viveWillAppear delegate method.
Every thing is in the same TakePhotoViewController.m file...
-(void) viewWillAppear:(BOOL)animated{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsEditing = NO;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
}
and the delegate methods:
#pragma mark -
#pragma mark - UIImagePicker delegate methods
//saving the image that was taken
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker release];
}
//alerting the user if the images was saved or not
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
//if user is cancelling the camera
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[self.tabBarController setSelectedIndex:0];
}
Thank you very much,
Erez
You're on the right track with the Cancel Action, but failed to do it for the other actions:
You need to call dismiss on ViewController after each action.
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html
EDIT: New link https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html#//apple_ref/doc/uid/TP40007070
... However, if you set this property to YES, your delegate must dismiss
the image picker interface after the user takes one picture or cancels
the operation.
So, if you need to take more than 1 picture, showCameraControls needs to be set to NO, and you need to use your own CameraOverlayView.
Also, definitely DO NOT release picker in your delegate! When the ViewController pops the view, it will release it automatically (Assuming that the picker is not being retained elsewhere). Rule of thumb, if you don't own (retain) it, don't release it. (This concept will probably be forgotten once iOS5 ARC is commonplace)
You should probably release the ViewController after it has been presented.
iPhone - modalViewController release
EDIT: more code to help
-(void) viewWillAppear:(BOOL)animated{
// No need to store... this is 1 use only anyway. Save memory, and release it when done.
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.allowsEditing = NO;
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
[imgPicker release]; // Release this here, this will execute when modal view is popped.
}
Delegate:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
/* Do what you need to do then */
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
//alerting the user if the images was saved or not
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
/* Do what you need to do then */
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
//if user is cancelling the camera
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
/* Do what you need to do then */
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

IOS development, using UITabBarController and UIImagePickerController, How to go from one tab to another?

I can't seem to find an answer for this, or maybe can't understand the things people wrote over the web...
I have a UITabBar with 3 tubs.
One of the tabs is a UIImagePickerController. This TabBar Item is connected to a view controller that i set also as the delegate for the Image picker (camera).
I want that then someone take a photo or press cancel, The first TabBar item will be choosen (don't want to stay in the TabBar that holds the camera).
My question is, How do I "talk" with the TabBar controller from a view controller that is in one of the TabBar items?
my code in the TakePhotoViewController.m file that is in the 3rd TabBer item and i want to go the first item.
-(void) viewWillAppear:(BOOL)animated{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsEditing = NO;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
}
and the delegate methods:
#pragma mark -
#pragma mark - UIImagePicker delegate methods
//saving the image that was taken
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Save image
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
[picker release];
}
//alerting the user if the images was saved or not
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
// Unable to save the image
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else // All is well
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
//if user is cancelling the camera
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}
Thank you very much,
Erez
[self.tabBarController setSelectedIndex:1];
You can pass your desired tab index value instead of 1.

UIImagePicker view not removing itself after first use

I have an app that is adding a view that is used to call UIImagePickerController. When the use hits the add image button, the following code executes:
'- (IBAction)addPhoto:(id)sender {
// Call background tap in case any keyboards are still up
[self backgroundTap:sender];
if (jpegData) {
// If we already chose an image, don't allow to choose another.
// Have to cancel out and come back!
return;
}
// Shows the photo picker so the user can take or select an image
photoPickerViewController = [[PhotoPickerViewController alloc] initWithNibName:#"PhotoPicker" bundle:nil];
photoPickerViewController.delegate = self;
// Add it to the subview - it will auto animate in/out
[self.view addSubview:photoPickerViewController.view];
}
This presents the user with a view that I created that has 3 buttons: Take photo, choose existing photo, and cancel. Cancel just cancels back to the main view. If take photo or choose existing is called, this code is executed:
'- (IBAction)choosePhoto:(id)sender {
// Show an image picker to allow the user to choose a new photo.
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
if((UIButton*)sender == chooseExistingButton) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.showsCameraControls = YES;
}
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
When if the user cancels out from the image picker, we go back to the main view. No problem. If however they complete image selection (either through taking a photo or choosing an existing one) then we call:
'- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Ok, dismiss the modal picker, bring up the activity indicator and dispatch a thread
// that will do all the photo processing ...
BOOL isFromCamera = picker.sourceType == UIImagePickerControllerSourceTypeCamera;
// Dismiss the picker view controller
[picker dismissModalViewControllerAnimated:NO];
// And remove our view from the list of views
[self.view removeFromSuperview];
if (isFromCamera)
{
// Declare the completion block to use
ALAssetsLibraryWriteImageCompletionBlock compBlock = ^(NSURL *assetURL, NSError *error) {
if (error != nil || assetURL == nil) {
NSLog(#"Failed to save photo: %#", error);
[delegate photoSetURLForImage:nil];
}
else {
NSLog(#"URL is : %#", [assetURL absoluteString]);
[delegate photoSetURLForImage:assetURL];
}
};
ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease];
[library writeImageToSavedPhotosAlbum:cgimage
metadata:meta
completionBlock:compBlock];
return;
}
else {
// Use the URL to get the metadata for the image that was picked ...
NSURL* url = [(NSDictionary*)info objectForKey:#"UIImagePickerControllerReferenceURL"];
if (url) {
// Define a result and failure block for fetching from the ALAsset
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset* myasset)
{
ALAssetRepresentation *rep = [myasset defaultRepresentation];
NSLog(#"URL is : %#", [[rep url] absoluteString]);
[delegate photoSetURLForImage:[rep url]];
};
// And also define a failure block
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
[delegate photoSetURLForImage:nil];
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:url
resultBlock:resultblock
failureBlock:failureblock];
return;
}
}
// If we get here, something went very wrong
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Error" message:#"An error occured while retrieving the image" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] autorelease];
[alert show];
}
Now, the first time I run through this code, all works great. As soon as I hit "use" on the image I took with the camera, or I select an image from the camera roll, the UIImagePicker view and the view that launches it go away and I'm back at the main view while I wait for the ALAsset calls to do their thing.
The problem starts when I try to repeat this process a second time. After selecting an image or taking one, the processing begins, but the views do not go away, until all of the processing by the ALAsset calls has completed. I can't figure out why this is. Why it works the first time, but not anytime after. Is there some caching mechanism I have to clear? Anyways, if anybody can offer up some advice, I'd be very grateful.
Thanks,
J
Yet again it appears I'll be answering my own question. This is starting to become a habit lol.
So it appears that the ALAsset calls aren't spinning off a new thread which is what I thought they were supposed to do. My bad. So to resolve, it was just a matter of spawning a new thread to do all the ALAsset stuff, and in the main thread dismissing my picker and what not.
Problem solved, yay!