Best way to use imagepickercontroller in iphone? - iphone

I am using UIImagePickerController to select the image from the PhotoLibrary in my application. I have used two different approaches for this. At first I have used a class variouble UIImagePicker with below code.
imagepicker = [[UIImagePickerController alloc]init];
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:self.imagepicker animated:YES];
Above code is working fine.But when I clicked on the button it is taking some time to react with the animation in this case.Then I used the autorelease pool approach with this method
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[[UIImagePickerController alloc]init]autorelease];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:picker animated:YES];
}
[pool release];
Also works charm. Both of them showing no leak in the analyser.Can anybody point me the right approach.

Well, no much to say here... Both approaches work, both approaches are correct, use whichever you prefer.
One minor point: if you are regularly presenting the image picker, you better use the first method, and assign it to an instance variable (it isn't called a "class variable"!) only for the first time, and don't release it until - dealloc - this way, you save the continuous allocation-deallocation of the image picker every single time the user chooses an image.

Related

Camera Filters in ios7

How I can use the camera filters that already exist in iOS7?
If i used this code:
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
the camera will open, and I can capture a photo but without using the filters.
my question is: can I use the exist filters in camera view in iOS7?
You should be able to use CIFilters. More info about CIFilters can be found here. Or you could use the awesome framework that's called GPUImage.

iPhone - Objective C - PLCameraPreviewView class

My app uses camera to take a photo.
When I open the camera the second time (after I opened it the first time and closed it),
the app crashes with this message:
*** -[PLCameraPreviewView isKindOfClass:]: message sent to deallocated instance 0x4193380
what is PLCameraPreviewView?
do you know what is happening, I just use the following code to open the camera:
self.imagePicker = [[[UIImagePickerController alloc] init] autorelease];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES];
The reason is that I have a UIView category which has a method:
+(UIView*)firstResponder;
when I change the name of this method to
+(UIView*)theFirstResponder;
the problem went away.
I guess we should not name the methods in our category similar to the built-in method names of that class.

Starting an iPhone app in camera mode shows just a blank screen

I'm creating an app for the iPhone which is supposed to start in camera mode (with a custom layout on top but that comes later). I've already created a version of this app where I press a button, that allows me to either choose a photo from the iPhone album or take a new photo.
Trying to use the same code in a different fails.
Originally I had a function 'takePicture' which I used to start the camera
- (void)takePicture
{
isInCaptureMode = YES;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
imagePicker.videoQuality = UIImagePickerControllerQualityTypeMedium;
// set delegate
[imagePicker setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
So I thought to myself "I can probably create a single view app, throw this code into the view controller's viewDidLoad function and use that as a starting point". But alas, I've been stuck there. When I do that the application starts and (if the device has a camera) the screen goes blackish blank, if the device does not have a camera (iPad or the simulator) the app crashes.
I originally created the app using Xcode 3 but have now migrated over to 4.2 the original app runs fine in 4.2 though (after I did a little bit of tweaking)
Can anyone explain why this happens?
can you try the following code to see what happen?
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
//you can get picture from Library is there is no camera
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
So, like I said in the comment. As soon as I called the code from 'viewDidAppear' instead of 'viewDidLoad' it finally ran...

how to close imagePickerController?

I just can't get this:
how in the world can I "shut down" imagePickerController on xcode?
This is the code that opens the camera, and it works perfectly..
-(IBAction) startcamera {
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsEditing = NO;
imagePickerController.showsCameraControls=NO;
imagePickerController.toolbarHidden=YES ;
imagePickerController.wantsFullScreenLayout=YES;
imagePickerController.cameraOverlayView=self.view;
self.imagePickerController.delegate=self;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
Ok now, say I want to close imagePickerController by clicking a button (means without picking any image): how am I supposed to do that?
Tried with:
-(IBAction)closecamera {
[imagePickerController release];
[imagePickerController dismissModalViewControllerAnimated:YES];
}
but it doesn't work!
Any clues?
Thanks in advance
Don't release imagePickerController twice.
You would dismiss it with [self dismissModalViewControllerAnimated:YES];
Don't release it twice and dismiss it before you release it. (like ole said)
But this is also important... as soon as you have released an object it is gone and no methods can be called.

Using UIImagePickerController in the game

I want to make a game, where the user can touch a picture of a TV and then choose a picture from their photo library.
I had success working with UITextFields, adding them to my EAGLView as subviews But I haven't been able to do the same thing with an UIImagePickerController , which seems the only way to do what I want to do.
Any help is appreciated !
UPDATE
With the suggestion of zpasternack I managed to make the menu appear by adding the UIImagePickerControllerDelegate, UINavigationControllerDelegate and using this method:
- (void) chooseImageFromLibrary {
if( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] ) return;
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.allowsEditing = YES;
[self addSubview:imagePickerController.view];
[imagePickerController viewWillAppear:YES];
[imagePickerController viewDidAppear:YES];
}
I call the chooseImageFromLibrary method at the start. It shows the interface, but as soon as i hit cancel or choose the app stops.
Can anyone suggest me:
1) How to make the cancel and choose buttons work.
2) How to call the UIImagePickerController whenever I want ( by calling chooseImageFromLibrary again ??).
3) When the user hits choose..Where do i find the UIImage the user just picked to ?, how do i pick it ?
Thanks.
I've never used one over an EAGLView, but UIImagePickerControllers are typically invoked with presentModalViewController, like:
- (void) chooseImageFromLibrary {
if( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] ) return;
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.allowsEditing = YES;
[self presentModalViewController:imagePickerController animated:YES];
}
Nervermind, problem solved. i just needed to overwrite some functions to make use of the UIImagePickerController.