When i open Camera in my app it works fine, but when i navigate in my app and comes back to camera view and again try to open my camera in iPad, app crashes and gives me "Received memory warning". app working fine in iPhone and this issues is only coming in iPad.
My project was without ARC so i converted my project into ARC in the hope to get good results but still my app crashes on camera after little navigation.
can anybody tell me how to decrease memory space with iPad camera so that my apps stop receiving memory warning.
this is my code for camera
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
imagePicker=[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = NO;
//imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:imagePicker animated:YES completion:nil];
}
This is where I get my Image
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[Array removeAllObjects];
[picker dismissViewControllerAnimated:YES completion:nil];
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[array addObject:image];
image=nil;
}
and i have also try to use popOverview for camera but it also didn't work.
In my viewController where I'm calling UIImagePickerController I have used 5 animations, before i was calling animation in viewWillAppear, and app was crashing so i changed Animation calling to ViewDidLoad and camera starting working but only until I navigate to my last view and comes back to open camera again.
I am getting same problem so i do like this.
Set UIImagePickerController as static like this.
static UIImagePickerController *imagePicker;
When you get image in this deleget.
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
#autoreleasepool {// to release memory
// Probelm is you image size.
// When you get this image it is vary large.
// And i hope you are creating multiple copy's of this image.
// when you get image in
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// In last set image as nil
image = nil;
}
}
Hope this will solve you problem.. :-)
Try to run your app without animation and then try, if it works then you need to improve your animations memory allocation, as animation needs a lot of memory.
Please try below code.try UIImagePickerController with in popover
if([UIImagePickerContrller isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.allowsEditing = YES;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
controller.delegate = self;
UIPopoverController *popController = [[UIPopoverController alloc] initWithContentViewController:controller];
popController.popoverContentSize = CGSizeMake(350.0f, 500);
[popController presentPopoverFromRect: self.button.frame inView:self.button.superview
permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES]
}
Related
In iOS 7 the camera comes with several modes: video, photo, square and pano. In the application I am developing, we allow users to use the camera to take pictures. We only want squared pictures so we make users crop images afterwards.
Is it possible to programmatically force the camera to only take squared pictures?
This is my code to open the camera:
-(void) openImagePickerSource:(UIImagePickerControllerSourceType)type
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = type;
[self presentViewController:imagePicker animated:YES completion:^{}];
}
I have been looking at the documentation but didn't find anything.
UIImagePickerController has a property called 'allowsEditing'. That will open the Camera in full screen and allows you to resize the taken photo after shot.
imagePicker.allowsEditing = YES;
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
you can get the photo in the protocol method using a key 'UIImagePickerControllerEditedImage'.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pic = (UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
}
iOS 7 using UIImagePickerController to take picture twice, at second time will show a static image covered the camera, how to reset the camera.
I'm try to take picture one by one, and keep take 5 pictures.
It works on iOS6.
on iOS7, it works fine at the first time, but when it take picture at second time, it will show a static dark image on screen, how to clear or reset it, although take picture works, but user can't see what will capture with camera.
bool fistTimeToShow = YES;
-(void) viewDidAppear:(BOOL)animated{
if (fistTimeToShow) {
fistTimeToShow = NO;
self.imagePickerController = nil;
[self tackImage];
}
}
-(void)tackImage{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerController = [[UIImagePickerController alloc]init];
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePickerController.showsCameraControls = YES;
self.imagePickerController.delegate = self;
[self presentViewController:self.imagePickerController animated:NO completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(#"====== imagePickerController:didFinishPickingMediaWithInfo ======");
[self.imagePickerController dismissViewControllerAnimated:NO completion:nil];
//...deal with the image capture...
self.imagePickerController = nil;
[self tackImage];
}
Update
I change the dismiss function to put the [self tackImage]; in block. And now it always show the fist image took.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(#"====== imagePicker: didFinish ======");
self.imagePickerController = nil;
[self dismissViewControllerAnimated:NO completion: ^{
[self tackImage];
}];
}
I'm trying to find a way to clear the image. But I don't know where the image saved yet.
Update2
use
[self performSelector:#selector(presentCameraView) withObject:nil afterDelay:1.0f];
and function
-(void)presentCameraView{
[self presentViewController:self.imagePickerController animated:NO completion:nil];
}
to replace. [self presentViewController:self.imagePickerController animated:NO completion:nil]; it works on my device anyway, but I don't even know why.
Update3
I have set the userInteractionEnabled to NO when Delay:1.0f to avoid other problems, and may be also need set the navigationBar and tabBar for specifically use.
I had exactly the same issue under iOS 7 using Xamarin.iOS.
What has helped, is adding GC.Collect() in didFinishPickingMediaWithInfo method.
In C# GC.Collect() "cleans up" the memory from unused/disposed objects.
For sure, there's no direct equivalent for that in Obj-C, but that may shed some light on your problem also (it could be memory-related).
I am using UIImagePickerController in an iOS app to save an image in context using UIGraphicsBeginImageContext/UIGraphicsBeginImageContextWithOptions.
I recently noticed that I picture I had saved and then displayed at a later date was highly pixelated; when I went back with the same code and imported the photo again, I got a great image. After playing with this for a while on my device, I figured out that the quality of the image saved depends on WHEN I pressed the 'Choose' button on the 'Move and Scale' screen.
If the image is a larger image and still loading when I press the button, the image is pixelated... if I wait until the image loads, it is fine. My question is, is there any way I can control when the user presses the 'Choose' button - is there any way to force them to wait until after the image is fully loaded? Or is there another way that would be better to approach this issue?
- (void)choosePhoto {
//NSLog(#"%s", __FUNCTION__);
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//NSLog(#"%s", __FUNCTION__);
[self dismissModalViewControllerAnimated:YES];
UIImage *pickedImage = (UIImage*)[info objectForKey:#"UIImagePickerControllerEditedImage"];
[self setPersonImage:pickedImage];
}
- (void) setPersonImage:(UIImage *)pickedImage {
//NSLog(#"%s", __FUNCTION__);
NSManagedObjectContext *context = [[UIApplication sharedDelegate] managedObjectContext];
PersonImage *oldImage = person.image;
if (oldImage != nil) {
[context deleteObject:(NSManagedObject*)oldImage];
}
if (pickedImage != nil) {
// Create an image object for the new image.
PersonImage *newImageObject = [NSEntityDescription insertNewObjectForEntityForName:#"PersonImage" inManagedObjectContext:context];
[newImageObject setImage:pickedImage];
[person setImage:newImageObject];
}
else {
[person setImage:nil];
}
NSError *error;
if (![context save:&error]) {
exit(-1); // Fail
}
}
I suggest you implement your own Crop&Resize view controller.
set imagePicker.allowsEditing = NO.
create your view controller in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info and pass selected image to your view controller.
push your view controller to image picker: [picker pushViewController:yourVC animated:YES]
The image captured by the iPhone 4 camera is ~5 MB in size and it takes a while to display/render it. One option is to compress the image using UIImageJPEGRepresentation().
If you do not want to compress the image, you can use UIWebView to display the images. The UIWebViewDelegate has a method - (void)webViewDidFinishLoad:(UIWebView *)webView, that hits after the rendering has been completed. You can enable the choose button in this method (which is disabled initially).
I have a simple application developed for android which takes a photo, accessing the camera of device.
I want the same application to be run on iPhone. For android we write permissions for accessing camera in manifest file. How do you access the camera in iPhone. Where do we need to set up it. Can anyone please help me?
Thanks in advance
Use the Media Capture API in the latest code, or wait for 0.9.6:
http://docs.phonegap.com/phonegap_media_capture_capture.md.html
For accessing the camera in iPhone, you can use following code.
// Camera
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.allowsImageEditing = YES;
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//[self presentModalViewController:imgPicker animated:YES];
[objParent presentModalViewController:imgPicker animated:YES];
[imgPicker release];
Let me know in case of any difficulty.
Below are the delegate method for camera.
Delegate Name: UIImagePickerControllerDelegate, UINavigationControllerDelegate
Methods:
#pragma mark UIImagePickerController delegate
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *imgCapturePhoto = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[imgPhoto setImage:imgCapturePhoto];
[self dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *) picker
{
[picker dismissModalViewControllerAnimated:YES];
}
Cheers
I'm soooooo close to finally finishing my first app to put in the store. Everything works just fine and memory leaks are almost totally nonexistent....except when I'm using the Camera or Selecting an Image from the Camera roll.
If the user chooses the camera vs. the roll....the camera works fine...takes a picture and then when they select "Use" it crashes. Same thing for the camera roll. I'm a noob so if I messed something up it wouldn't surprise me. Any help/suggestions greatly appreciated...here's the code:
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhoto) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
//[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
theimageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[picker release];
}
Your problem might be, since you use the original image, since its something like 1400x750 (not sure about the exact dimensions), you are probably running out of memory when you are setting it as the image of the imageview to be displayed...You should probably resize your image to 320x480 or 480x320 to display it in the image view, that will probably fix your problem.
The only issue that jumps out at me is that UIImagePickerControllerOriginalImage is an NSString constant, so you don't want to put it in quotes:
theimageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
But even if that line were to fail, it would only set theimageView.image to nil which probably shouldn't cause a crash. You should see at least some more info about the crash in the Xcode Console, which will help. Also, check out the tips in this SO answer.
Change
[picker dismissModalViewControllerAnimated:YES];
to
[self dismissModalViewControllerAnimated:YES];
That should work