imagePickerController didFinishPickingMediaWithInfo giving me a weird image - ios

I wrote some code to add a photo taken with the camera and add it to a NSMutableArray of UIImages.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.description.text = [NSString stringWithFormat:#"image is %d by %d", image.size.width, image.size.height];
//[self.imageArr addObject:image]
//[self saveImages];
}
The "self.description.text" is a UILabel that I just use to get information when I test on my iPhone (my macbook is too old to upgrade to the last OS and version of Xcode).
I found that when I add the image to my imageArr, the app crashes (I know that imageArr is not nil, I've used the description UILabel to check).
If I comment out the addObject and saveImages functions, I found that, after I take a photo, the description states that the UIImage has a width of 0 and height of 1084850176. Has anyone had this problem before? I've been trying to take and save photos within the app for a few days now, and I can't seem to solve this problem.
EDIT:
I fixed the image dimensions problem by using %f instead of %d (thanks idz!).
It's still crashing when I uncomment the [self.imageArr addObject:image] line. The crash report says the exception type is EXC_CRASH (SIGABRT) when running that line. I don't really know what this means, but I'm sure that imageArr is not nil and it is an NSMutableArray, so I don't see why it keeps crashing there.

Usually you have to allocate and initialize self.imageArr.
That's what usually causes those errors. Also if it's an array make sure it's a mutable array.

Related

iPad image is getting cropped/resized while using image picker controller in objective c

When I use image picker to take photos using iPad camera , the photos are automatically getting resized/cropped while storing it.The same code is working fine with iPhone.Can anyone please help me to find out a solution for this. I am getting this in iOS 9.x.I need original images to be displayed.
Thank you very much in advance.
My issue got resolved when I changed
UIImage * image = info[UIImagePickerControllerEditedImage];
to
UIImage * image = info[UIImagePickerControllerOriginalImage];
in didFinishPickingMediaWithInfo method.

UIImage imageNamed memory leak

I am trying to understand why I have memory leaks in a very basic implementation of a UIImage and a UIImageView.
I am not using ARC in that case (correctly disabled).
My code is pretty straightforward :
UIImage *image = [UIImage imageNamed:#"my_image.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[[self view] addSubview:imageView];
[imageView release];
I am implementing this code in the viewDidLoad: method of a UIViewController.
By calling the method imageNamed: of UIImage, I know that I will get an object that I do not own / an autorelease object. This object will also be retained by the UIImageView object instantiated just after. So the only object I have the ownership is the UIImageView one.
After running this app with the Memory Leaks Instruments, I have this report :
I heard about the cache system that operates but I should not have have memory leaks because some datas are cached.
Here is a reference to the answer with the cache explanation :
https://stackoverflow.com/a/2930567/1154501
Thank you in advance !
Edit : Tried also with ARC, and I got the same issue.
[UIImage imageNamed:] is managed by the operating system. Deallocating the UIImage created from this method might free up the memory that was allocated. If you have lots of images or user generated content, you should be using [UIImage imageWithContentsOfFile:] or [UIImage imageWithData:].
If you create too many images with [UIImage imageNamed:], your app may get killed by iOS because of memory usage. I made a sample app to prove this to myself, see more here: iOS UIImage storage formats, memory usage and encoding / decoding
Did you try to open the 'Extended Detail' right panel and look for the exact line where the memory is leaking?
Your code is OK, my opinion is that the leak is somewhere else.

UIImagePickerController Cropping?

I have read that it is automatic but it doesn't appear to be happening in my case. Using UIImagePickerController with allowsEditing set to YES I get my editing view with the crop square overlay, but when i am finished with the image its not cropped as I expect.
Should it be automatic at this stage? If not, what might be a good solution to crop the image in the matching area that the default view provides?
Thanks.
You are probably using the wrong key for the info dictionary: UIImagePickerControllerOriginalImage.
To get the cropped (yes, it is automatic) image you have to use UIImagePickerControllerEditedImage key.
This should do the trick:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *pickedImageEdited = [info objectForKey:UIImagePickerControllerEditedImage];
//dismiss the picker and process the image...
}

Strange issue with loading an UIImage from core dara directly using KVC

I am in the middle of an app project with core data where I stricty use KVC only with regard to the NSManagedObjects. The purpose of consequentally doing that is simply to get an understanding of advantages and disadvanages. I may have discovered a disadvangate. However, I cannot explain why.
I came across this when I used some very well established principle (used that in other apps already) of saving a scetch that the user drew with his fingers (a signature to a document) in core data and fetch and display or print to PDF respectively it later.
It turned out that I was able to get the UIImage from the view, store it in core data and instanly receive it from core data again using KVC (no save/fetch in between) and display it in another view for testing purposes. But I was unable to actually save it, fetch the data from persistant storage and re-display it on the screen.
So I did a lot of debugging, analysed the sqlite file etc. Eventually I nailed it down to the following strange behaviour:
Situation:
The image was stored into core data successfully by doing this:
[self.detailItem setValue:UIImagePNGRepresentation(image) forKey:kSignatureImage];
with image being an UIImage object and self.detailItem is the NSManagedOBject. Keys and references and everything is fine.
Then it comes to displaying the view again. The following is independent from whether the context was saved in between or even the app was closed and restartet or not:
self.signatureCanvas.image = [UIImage imageWithData:[self.detailTextLabel valueForKey:kSignatureImage]];
with self.signatureCanvas beeing an UIImageView subclass object. The image is not shown on screen.
NSData *data = [self.detailItem valueForKey:kSignatureImage];
NSLog(#"UIImage a: %#", [UIImage imageWithData:[self.detailTextLabel valueForKey:kSignatureImage]]);
NSLog(#"UIImage b: %#", [UIImage imageWithData:data]);
self.testImage.image = [UIImage imageWithData:[self.detailTextLabel valueForKey:kSignatureImage]];
self.signatureCanvas.image = [UIImage imageWithData:data];
self.testImage is a second plain UIImageView withn the same view (but smaller). Now the image is visible in signatureCanvas but not in testImage. (could be vice versa, tried that)
The output give a hint on what was happening but no actual explanation:
2013-03-22 12:43:37.746 MyApp[1595:c07] UIImage a: (null)
2013-03-22 12:43:37.769 MyApp[1595:c07] UIImage b: <UIImage: 0x7c973a0>
Until now I would have thought that the lines of code were kinda equivalent and that the compiler may even optimize the data object away. But why is the image (null) in one case and not in the other.
So I found the problem root cause and a workaround. Therefore this is not urgent any more. But I do not understand it. Any clues?
Environment is: SDK 6.1, xcode 4.6, ARC, core data on sqlite, iPad only so far. All this happens in a UITableViewCell subclass (prototype cell within storyboard) naturally in a UITableViewController subclass.
I assume that self.detailTextLabel in
self.testImage.image = [UIImage imageWithData:[self.detailTextLabel valueForKey:kSignatureImage]];
should be self.detailItem.

Wrong version of image from UIImagePickerController

I have a problem with the UIImagePickerController component. Currently in my app the user can pick an image from the saved photos library with the picker, no problems there.
However, it seems that if I crop and save a photo in Photos.app before picking the image, UIImagePickerController gives me the original uncropped version in the UIImagePickerControllerOriginalImage dictionary key.
I understand that UIImagePickerControllerEditedImage works when the crop is done inside the picker, but when done in the Photos app this key returns nil.
So my question is, how do I access the correct version of the image (without rolling my own picker with ALAssetLibrary)?
The solution was simple; I was using UIImagePickerControllerSourceTypeSavedPhotosAlbum instead of UIImagePickerControllerSourceTypePhotoLibrary.
What is your
-(void)imagePickerController:(UIImagePickerController *)pickr didFinishPickingMediaWithInfo;
method like?
Have you tried to do something like
-(void)imagePickerController:(UIImagePickerController *)pickr didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
[myImageView setImage:image];
[pickr dismissModalViewControllerAnimated:YES];
}
(in the example it's UIImagePickerControllerEditedImage, don't know if you set YES to your picker allowEditing)

Resources