Image size on Objective-C - ios

How to get Image size in Objective-C.
I loaded one image using imagePicker, I want know the size of that image. (w*h)
is it possible?
Thanks & regards...

You implement the UIImagePickerControllerDelegate Protocol and in particular the imagePickerController:didFinishPickingMediaWithInfo: method. The last parameter in that method is a dictionary that includes an UImage references by the UIImagePickerControllerOriginalImage key. Get this UIImage* and get it's size property.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage];
//access width and height like this
image.size.width;
image.size.height;
}

Related

How To implement double exposure for picking images?

In my app I'm having two imageviews, first imageview will be constant and for second imageview ,after picking image it will zoom and it can be place any where in the view and second image will be merged with first image and after merging second imageview will be nil and first imageview will be having two images and we can pic as many images in second imageview and can move anywhere but we need pic only single image at single time.How to implement this.?.Thanks in advance.
A simple image usually has 32bit of color information per pixel (rgb: ff ff ff), a grayscale image has 8bit.
It should be obvious that a conversion from 32bit to 8bit is not reversible.
To achieve your goal you have to keep a copy of your original Image.
When you select Image from gallery save it in one UIImage object as original image before applying any grayscale on that.
Make another copy of selected Image and apply grayscale on that and assign this image to ImageView.
Now when you touch on this ImageView (Detect touch using Gestures) set originalImage to the ImageView.
Edit:
You can have function like this:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
[picker release];
// Edited image works great (if you allowed editing)
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
// AND the original image works great
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
// AND do whatever you want with it, (NSDictionary *)info is fine now
}
In above code you can declare both editedImage and originalImage variables at global scope, assign editedImage as image for your ImageView and on touch on ImageView you can show originalImage

image returned from UIImagePickerController is smaller than original

Im trying to get a full sized image from the photo library using UIImagePickerControllerOriginalImage.
the returned UIImage size is 750 x 1001, but when i extract the image using Image Capture, it is actually 3264 x 2448, how can I get the real original image?
When you initialize the UIImagePickerController make sure to set allowsEditing property to NO, which would allow you to get the full-sized original image.
For an example, if you use:
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
self.imagePickerController.allowsEditing = YES;
the resulting image you get in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info from info[UIImagePickerControllerOriginalImage] will have it's size less than the actual original, like it's happening to you.
But if you set self.imagePickerController.allowsEditing = NO;, UIImage from info[UIImagePickerControllerOriginalImage] will be the exact same size as it's in the library.

NSData from UIImage. Uploading to server with wrong orientation

I'm trying to upload UIImage which taken from UIImagePickerController using UIImageJPEGRepresentation. But it shows on server with wrong orientation. First of all, I'd like to ask is there way to fix it on my side? Or if not how to handle it on server?
I already saw iOS 4 Image Oriented Incorrectly When Posted To Server and this one Save UIImage, Load it in Wrong Orientation but I need to show image with right orientation directly on server not in application.
From what I've found, this seems to be the best answer.
iOS UIImagePickerController result image orientation after upload
Add the category UIImage+fixOrientation to your project and import "UIImage+fixOrientation.h" in your view controller.
Then you should be able to use something like this.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
image = [image fixOrientation];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
}
try
UIImage *orientationFixedImage = [UIImage imageWithCGImage:[originalImage CGImage] scale:1.0 orientation:originalImage.imageOrientation];

How to use UIImage+SDAdditions

I am trying to use the above category for UIImage - Found Here
The reason for use is that I am having the user take a photo using the Camera/Picker. I then need to display the image selected in a UIImageView.
I would like to keep the orientation information of the image but scale it down slightly, for performance and best fit to the image view.
How do I implement to use this category? I am unsure how to make the call to the cat, do I call the method in some way?
At present, I have some delegate methods for the picker:
#pragma mark - UIImagePickerDelegate Methods
//delegate methode will be called after picking photo either from camera or library
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.collectedImage = image;
NSLog(#"the collect image orientation is: %i",image.imageOrientation);
self.collectedImage = image;
NSLog(#"--------------------collected image information----------------------");
NSLog(#"the collected image orientation is: %i",image.imageOrientation);
NSLog(#"the image width is: %f",self.collectedImage.size.width);
NSLog(#"the image height is: %f",self.collectedImage.size.height);
// scale the image
UIImage *newCollectedImage = [self.collectedImage scaleWithMaxDimension:([[UIScreen mainScreen] bounds].size.height)];
self.collectedImage = newCollectedImage;
NSLog(#"--------------------collected NEW image information----------------------");
NSLog(#"the new collected image orientation is: %i",self.collectedImage.imageOrientation);
NSLog(#"the new image width is: %f",self.collectedImage.size.width);
NSLog(#"the new image height is: %f",self.collectedImage.size.height);

Rotation after select UIImage of library

When I chose a portrait photo from the library like that :
(source: hostingpics.net)
The UIImage returned by the library is rotated.
Here is my code :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
UIImage* originalImg = [editInfo objectForKey:UIImagePickerControllerOriginalImage];
[self useImage:originalImg];
}
How can I avoid that ?
self.imageData=[info objectForKey:#"UIImagePickerControllerOriginalImage"];
CIImage *image = [[CIImage alloc] initWithImage:[info objectForKey:#"UIImagePickerControllerOriginalImage"]];
self.imageData=[UIImage imageWithCIImage:image scale:self.imageData.scale orientation:self.imageData.imageOrientation];
You can use the below methods to retain the image's orientation:
[UIImage imageWithCIImage:(CIImage *) scale:(CGFloat) orientation:(UIImageOrientation)];
[UIImage imageWithCGImage:(CGImageRef) scale:(CGFloat) orientation:(UIImageOrientation)];
Did you previously take the image using an image picker in your app and then save the image?
If so, did you set the orientation when you saved the image? You need to.
Consider adding a log statement or debugging to check what orientation is set on the image.
If the orientation is set on the image then UIImageView will automatically render the image correctly.
If (in the future) you're going to take the image data and upload it somewhere then you would need to check the orientation and re-draw the image while applying an appropriate transform to the context.

Resources