Reputation: 3522
I have seen many questions here about taking photo with this api
but I run into specific problem
with following code
.h file
@interface ComposeMViewController : UIViewController <UITextFieldDelegate , UITableViewDelegate ,UITableViewDataSource, UIImagePickerControllerDelegate , UINavigationControllerDelegate , MFMailComposeViewControllerDelegate , MFMessageComposeViewControllerDelegate , DistributionListViewControllerDelegate,UITextViewDelegate>
in .m
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:_imagePicker animated:YES completion:nil];
} else {
NSLog(@"Camera not available");
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"At 1");
UIImage *fullImage = [info objectForKey:UIImagePickerControllerOriginalImage];
_attachment = [NSMutableDictionary dictionary];
NSData* imgData = UIImageJPEGRepresentation(fullImage,0.0);
[_attachment setObject:@"atachmentINFOR.jpg" forKey:@"ImageName"];
[_attachment setObject:imgData forKey:@"ImageData"];
NSLog(@"%@",info);
[self dismissViewControllerAnimated:YES completion:nil];//warning disabled
}
I am receiving memory warning now , can some1 know why ?
Upvotes: 1
Views: 736
Reputation: 7022
This might help someone else - I know this is an old thread but I've been experiencing a memory leak myself in regards to the camera. I ran instruments on it (would recommend) and found that the responsible frame is: [UIImagePickerController viewWillDisappear:]
. This belongs to the UIKit
library so I am afraid there is little you can do to correct the memory leak. Its a bug in UIKit
. Im using iOS7.1 on the iPhone 5s. I would recommend using a memory efficient library,such as SimpleCam that can be found on GitHub.
Upvotes: 0
Reputation: 4513
You should always check if source type is available per to the documentation. As @Programming Thomas said make sure UIImagePickerControllerDelegate, UINavigationControllerDelegate delegate have been set..
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:nil];
} else {
NSLog(@"Camera not available");
}
if you happen to get a error message like "Warning: Attempt to present on whose view is not in the window hierarchy!" usually happens when viewContoller has not completely loaded yet wait for a second or so. maybe to use dispatch timer after half a second
Upvotes: 1
Reputation: 1608
The most likely explanation is that your UIViewController doesn't respond to the UINavigationControllerDelegate or the UIImagePickerControllerDelegate (it must respond to both in order to be the delegate for the UIImagePickerController.
Your second attempt isn't compiling because SourceType
isn't a property of UIImagePickerController - it is sourceType
. By convention, Objective-C/Cocoa uses lowercase letters at the beginning of variable and property names.
Upvotes: 1