I have seen this question being asked many times before but I can't seem to get an answer that helps me. So here is my situation :
I have a user press a button that asks them if they want to take a picture or choose one from their album. When testing on my actual phone the choosing from album mode works, but the picture from camera goes all weird.
What happens : User is shown camera view -> user takes picture -> CRASH
The crash is not one in the console tho... I get a window on top of my xCode window saying :
App stopped unexpectedly due to Memory Pressure.
So I read that I need to resize the image I am getting from the camera before I display a small thumbnail for the user... I am doing that but to no avail... this has now become a consistent issue.
Here is my code :
(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissViewControllerAnimated:YES completion:nil];
//UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
CGRect rect = CGRectMake(0,0,320,480);
UIGraphicsBeginImageContext(rect.size);
//[img drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[_photoTaken setImage:picture1];
[self updateAppropriateCharacterCount];
[_userInputTextField becomeFirstResponder];
}
Let me explain what the variables are :
_photoTaken is a pointer to a UIImageView where I want to display the thumbnail. It is much smaller than 320 x 480 though.
Now I also have this method :
(void) takePhotoButtonPressed {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}
Finally before I did all this funny resize stuff I was setting the "img" variable to another pointer called "image" that was defined in my local implantation. I was using this to check (later in the code) if an image existed.
Any help would be appreciated, or some guidance.
Thank you much
EDIT
I also noticed that if I run my app for some time before going right to taking a picture it crashes before even showing the camera - I press take a photo and bam dead....
I have a project designed to be more memory efficient for taking pictures. The files will be larger than thumbnail, but it usually runs much lighter than UIImagePicker, and the final picture files are significantly smaller.
You can find it here
Related
I take photos using UIImagePickerController, source type is UIImagePickerControllerSourceTypeCamera.
After i captured, the image editing is changing depending upon the orientation. Herewith i attached my snapshot.
Image 1 : i took this image in portrait mode.
Image 2 : i took this image in landscape mode. here, the top portion is in black color.
My code is :
UIImagePickerController *pickerCamera = [[UIImagePickerController alloc] init];
pickerCamera.delegate = self;
pickerCamera.allowsEditing = YES;
pickerCamera.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:pickerCamera animated:YES completion:NULL];
I want to rotate the image to portrait. Please help me to solve this issue.
I'm writing app now which is using uiimagepicturecontroller.
Task is very simply. U need to take a picture a thing inside place in overlay (overlay is just a circle) and then display it. It works fine, when user holding the iPhone vertical. Picture is cropping and displaying fine. Problem is when user rotate their iPhone horizontal. I suppose camera is rotating and i can't display photo as i'd like.
Any ideas how to remove autorotatin?
Device orientations in my target->general is only Portrait
I tried override autorotate method in UIImagePickerController and it didn't works
Any idea how to fix it? Or any tricky tricky method to display image correctly?
Best regards,
David.
EDIT: Solution for this is use AVCam and write "own" class to take a picture.
i think it works:
-(void)imagePickerController:(UIImagePickerController *)pickerinput didFinishPickingMediaWithInfo:(NSDictionary *)info {
[pickerinput dismissViewControllerAnimated:YES completion:nil];
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
UIImageView *yourImgView=[[UIImageView alloc] initwithimage:image];
//please add this
yourImgView.transform = CGAffineTransformMakeRotation(M_PI/2);
}
Try this.
_img = [UIImage imageWithCIImage:_image scale:_imgScale orientation:_imgOrient];
_imgOrient = _img.imageOrientation;
_imgScale = _img.scale;
I have a routine that I use to trigger the camera to take a photo. After the camera takes the photo the user is then given an option to crop the image by default before the delegate passes me back the UIImage. Is there any way that I can pass in a dynamic CGRect to make this default crop area a specific size? One thing I should mention is that the application is a landscape iPad application.
Here is a code sample:
-(void)triggerCamera:(id)sender
{
UIImagePickerController *camera = [[UIImagePickerController alloc] init];
camera.delegate = self;
camera.allowsEditing = YES;
camera.sourceType = UIImagePickerControllerSourceTypeCamera;
//pass in some sort of CGRect ??
[self presentViewController:camera animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
UIImage *img = [info objectForKey: UIImagePickerControllerOriginalImage];
//process my image further
}
Unfortunately you can't set crop size. I was dealing with same problem over 2 years ago and I got stuck with creating my own viewController for cropping image.
Maybe take a look at GKImagePicker on GitHub. This project hasn't had much activity in the past few months (maybe more), but could be worth a shot. It even comes with an option to have a resizable crop area. I have not tried it myself, but the implementation looks to be pretty simple:
self.imagePicker = [[GKImagePicker alloc] init];
self.imagePicker.cropSize = CGSizeMake(320, 90);
self.imagePicker.delegate = self;
[self presentModalViewController:self.imagePicker.imagePickerController animated:YES];
I have a modal which displays a UIImagePicker and allows the user to take a photo which then gets put into a UIImageView.
Every now and then I get a memory warning 'Received Memory Warning' and the UIImageView does not get assigned. The App is fairly simple and it's not using a lot of memory, and it seems that a lot of the time this has to do with the ImagePicker as a separate process. This only happens when using the camera, and it happens about one in five times.
There's a lot of talk about this online, and most of the answers say to 'handle the warning appropriately'; but I"m not sure what that means - I just want the taken photo to show up in the ImageView! It seems to happen before I can do anything about it in the UIImagePicker delegate.
What can I do to mitigate this?
Here's the didFinishPickingImage Delegate:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {
//set background for image select button, put image reference somewhere to copy image to documents folder and insert into modular's sentence object.
//ImagePicker.jpg needs it's border set to 1px black.
NSLog(#"imagePickerController: selectedimage: %#", selectedImage);
imageViewBehindPhotoBigButton.clipsToBounds = YES;
[imageViewBehindPhotoBigButton.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageViewBehindPhotoBigButton.layer setBorderWidth: 1.0];
[imageViewBehindPhotoBigButton setBackgroundColor:[UIColor whiteColor]];
[imageViewBehindPhotoBigButton setContentMode: UIViewContentModeScaleAspectFill];
imageViewBehindPhotoBigButton.image = selectedImage;
imageViewBehindPhotoBigButton.hidden = NO;
[choosePhotoBigButton setBackgroundImage:nil forState:UIControlStateNormal];
choosePhotoBarImage.hidden = NO;
choosePhotoText.hidden = NO;
addButton.enabled = YES;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//because iPad uses a pop up - we don't want to dismiss modals here!
[imagePopupController dismissPopoverAnimated:YES];
}else{
[self dismissModalViewControllerAnimated:YES];
}
}
The answer was to implement threading when scaling / handling the image from the UIImagePickerController. There's a really good discussion that I used here.
I am working on an application for iPad, it is working fine until i reached this point:
The app shows the popover for the photo library, but when I choose the photo, the popover doesn't hide, and I also want it to view the selected image in a UIImageView, however i do not know how.
I am sure there is something wrong in the didFinishpickingMediaWithInfo function. here is the function's code:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishpickingMediaWithInfo:(NSDictionary *)info{
//bgImage is a UIImageView
bgImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Dismiss UIImagePickerController and release it [picker dismissModalViewControllerAnimated:YES];
[picker.view removeFromSuperview];
[picker release];
}
My first question is: What am I supposed to add to this function for viewing the selected photo in the UIImageView? (When I click on the photo from the photo library in the simulator, neither the photo library hide nor the image is viewed in the specified UIImageView)
2- I have read that I should've used UIImage instead of UIImageView.. Is this true? If yes, what about the interface builder? There is nothing called UIImage?
To close the image picker, use:
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
What you get from
bgImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
is not an UIImageView, it's a UIImage. To get it displayed, you need to have a UIImageView in your UI somewhere, and set the view's image with what you just got from the picker:
imageView.image = bgImage
Hope this helps