this is a well known snippet, how to select a picture from the iPhone photo library:
- (IBAction)selectExistingPicture {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
}
Here you can see a screenshot of Instruments (fullscreen).
alt text http://img.skitch.com/20090624-rtqp2mgsnyynkgb97c9e8d2g9c.jpg
Why does it leak? I don't understand it, because picker is released properly, I think.
The UIImagePickerController is known to leak. If you are going to use it more than once it's recommended that you reuse a single instance
You are presenting picker but then losing the pointer to it when you leave the method. As it is allocated memory, that is your leak. Try:
UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
Shouldn't you be doing an autorelease on the UIImagePickerController?
UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
Related
I am working in phonegap Camera API new to objective c i dont know how to declare method and interface for this function in .h file
if([(NSString *) [components objectAtIndex:1] isEqualToString:#"Take_Photo"])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
I've declared UIImagePickerControllerDelegate in .h file but the camera is not opening.
i did not get any error when i debugging the application paused after completed the line
[self presentModalViewController:picker animated:YES];
the application debugger paused in the binary file. i think i missed something in .h file.
#interface NativeAPI :UIViewController
<UIWebViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
Reachability* internetReachable;
Reachability* hostReachable;
UIImagePickerController* Image_Picker;
}
any one help me thanks in advance
You will be required to assign the value of the picker to the instance variable like this.
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
self.Image_picker = picker;
[picker release];
Image_picker.delegate = self;
Image_picker.allowsImageEditing = YES;
Image_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
Try the code above
You can define a method under the implementation which is to be called by the delegate.
I am trying to do this .I know it very simple question .
Take Video: Take Picture: Choose from Library: Everything I find has separate buttons. Kinda like in the facebook app when you click on the camera button you get this picture.
I think people will me as soon as possible
Thanks in advance
To take Picture, video and select an image from photo library, UIImagePickerController will help you.
Ref: UIImagePickerController
To take picture and video, UIImagePickerControllerCameraCaptureMode has to be set properly.
Also the sourceType needs to be set as we required. See UIImagePickerControllerSourceType.
I guess these summary will help you. See the reference for more details.
If u want to browse thru ur photo library,
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
If u want to take new photo,
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker. cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
If u want to take new video,
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker. cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
[picker release];
I can't believe this wasn't wrapped up in a little class. So I made one
https://github.com/fulldecent/FDTake
Example there shows taking a picture and choosing a photo from library.
Hi all I am implementing the following code to take a photo through my application
[[[UIApplication sharedApplication] keyWindow] setRootViewController:picker];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:NO];
Can anyone tel me How I can go to the photo gallery after the user takes the photo and not return to the application.
i think you might want to try this
How to open camera while I click the UIButton in iPhone?
or i really didn't get the question
Edit:
Well you can use the same code with UIImagePickerControllerSourcePhotoLibrary after taking the pic from camera. I mean call this Function with UIImagePickerControllerSourcePhotoLibrary then it will take you to photo Library and then back again to your application.
UIImagePickerControllerSourcePhotoLibrary
If you want to shoot a picure use this code -
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
picker.allowsImageEditing = YES;
[[self navigationController] presentModalViewController:picker animated:YES];
If you want to go to photo gallery and wants to select a photo from there use this code -
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
picker.allowsImageEditing = YES;
[[self navigationController] presentModalViewController:picker animated:YES];
Notice the difference between the picker's sourceType
I'm doing an application which allows user to take a picture from camera or select picture from library.
I'm using the code
- (BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[[UIImagePickerController alloc] init]autorelease];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsImageEditing = YES;
picker.delegate = self;
[controller presentModalViewController:picker animated:YES];
}
return YES;
}
I'm getting memory leak when I'm running this application. I'm running this application on 3.0.
Guys Please help me.
After [controller presentModalViewController:picker animated:YES]; do [picker release]; and get rid of the autorelease when you init the UIImagePickerController. That may work?
Thanks to all for responding to the questions which I posted.
I got one problem that is, while capturing the video in the iPhone, I don't know how to store the time period (duration) that I captured video with iPhone. Can any one solve my problem.
I am using the following code for capturing.
-(void) RecordVideoWithCamera
{
printf("\n Hai I am in record vedio with camera -============");
[self startCameraPickerFromViewController:self usingDelegate:self];
}
- (BOOL)startCameraPickerFromViewController:(UIViewController*)controller usingDelegate:(id<UIImagePickerControllerDelegate>)delegateObject
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[[UIImagePickerController alloc] init]autorelease];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeMovie];
picker.delegate = self;
picker.showsCameraControls=YES;
picker.allowsEditing = NO;
UIView *overlayView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
picker.cameraOverlayView = overlayView;
[controller presentModalViewController:picker animated:YES];
}
return YES;
}
Thanking you,
Madan Mohan.
I think you should check CMTime struct.