How to get the image width and height from image view in iOS - ios7

i have one image as 640*960,and my image view has 300*300.i set the content mode the image view as aspect fit, now i load the image to the imageView it looks follow..
after loaded the image to the image view i need the width and height of the image from the imageView.

Did you try?
#import AVFoundation;
UIImageView *image = [[UIImageView alloc] init];
image.image = [UIImage imageNamed:#"paint5.png"];
CGRect yourFrame = AVMakeRectWithAspectRatioInsideRect(imageView.image.size, imageView.bounds);
NSLog(#"%#",NSStringFromCGRect(yourFrame));
I think thats what u are looking for.
AV Foundation Functions Reference

You can get height and width of image in following way:
UIImageView *image = [[UIImageView alloc] init];
image.image = [UIImage imageNamed:#"paint5.png"];
int imgHeight = image.layer.frame.size.height;
int imgWidth = image.layer.frame.size.width;
NSLog(#"height of image %i",imgHeight);
NSLog(#"widht of image %i",imgWidth);

Related

UIImageView is resized as per UIImage - Objective C

I am making UIImage from CGIImageRef.
CGImageRef drawImage = CGImageCreateWithImageInRect(croppedImage.CGImage, frame);
UIImage *newImage = [UIImage imageWithCGImage:drawImage];
CGImageRelease(drawImage);
And applying it to Image View:
self.imageview.image = newImage;
After this line self.imageview.image = imgV.image; my imageView is resized to size of UIImage(newImage).
When I log my self.imageview frame then I am getting correct value. I am not able to get exact reason behind this.
UIImageView will be autoResize to fit its image size if u didnt set the frame of imageView

roundedCorner on UIImageView applies incorrectly to UITableCell

Hi there I have a table cell which contains amongst other things a UIImageView. I want to make this UIImageView have a rounded corner. Here's the code that I have (relevant snippets):
UIImageView* image;
image = (UIImageView*)[cell viewWithTag:0]; // get the UIImageView from the nib
image.image = [UIImage imageWithData:imageData]; // imagedata contains the image
image.layer.cornerRadius = 10.0;
image.layer.masksToBounds = YES;
image.clipsToBounds = YES;
When I do this, the entire UITableViewCell gets the rounded corners, instead of the UIImageView (see attached image).
Can someone tell me why the rounded corners are applied to the table cell rather than the uiImageView?
http://i.stack.imgur.com/CnkyQ.png
(UIImageView*)[cell viewWithTag:0];
In cell subviews you have only UITableViewCellContentView with tag == 0.
You can manually add the UIImageView to cell.
The problem should be the ...Tag:0:
image = (UIImageView*)[cell viewWithTag:0]; // get the UIImageView from the nib
If you are using a non-custom UITableViewCell replace that line with:
image = (UIImageView*)[cell imageView];
Else, replace the tag number.

Loading a png into a UIImageView iOS

I have two images that need to be overlaid over one another, and they are both png images (since I need to be able to make them transparent). However, when I load them into a UIImage view in my xib file, neither of them display at all! When I try using the jpg format of the same images it works fine, but because jpg doesn't support transparency, the overlay effect I need is lost. How can I get the png images to actually display in the window?
This is the kind of task that is easier to do from code than from Interface "Crappy" Builder:
CGRect imageFrame = CGRectMake(x, y, width, height);
UIImage *image1 = // however you obtain your 1st image
UIImage *image2 = // however you obtain your 2nd image
UIImageView *imgView1 = [[UIImageView alloc] initWithImage:image1];
// Adjust the alpha of the view
imgView1.alpha = 1.0f; // This is most advisably 1.0 (always)
imgView1.backgroundColor = [UIColor clearColor];
imgView1.frame = imageFrame;
UIImageView *imgView2 = [[UIImageView alloc] initWithImage:image2];
// Adjust the alpha of the view
imgView1.alpha = 0.5f; // or whatever you find practical
imgView1.backgroundColor = [UIColor clearColor];
imgView2.frame = imageFrame;
// Assume a view controller
[self.view addSubview:imgView1];
[self.view addSUbview:imgView2]; // add the image view later which you wanna be on the top of the other one
// If non-ARC environment, we need to take care of the percious RAM
[imgView1 release];
[imgView2 release];
Try to open your png images in a photo editor like photoshop or pixelmator and save it again as NOT interlaced (in the save options of png).

iPhone: image horizontal gallery

I am trying to create sample gallery before filling real data.
float xAxis = 0;
float mostHeight = self.galleryView.bounds.size.height;
for (int i = 0; i < 5; i++) {
//getImage from url
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString: #"http://4.bp.blogspot.com/-M9k3fhlUwmE/TdYgxL97xMI/AAAAAAAAAY8/_r45zbAm1p0/s1600/CARTOON_Cat-full.jpg"]]];
myImage = [[MyMath sharedMySingleton] imageWithImage:myImage convertToSize:CGSizeMake(mostHeight - 10, mostHeight - 10)];
//set image
UIImageView *rview= [[UIImageView alloc] initWithImage:myImage];
/*adding the view to your scrollview*/
[self.galleryView addSubview:rview];
xAxis += myImage.size.width + 20;
}
//set scrolling area
self.galleryView.contentSize = CGSizeMake(xAxis, mostHeight);
galleryView is UIScrollView from xib. Here, i am trying to fill 5 same pictures to gallery, but it fills only one and space after that (because of xAxis number). What did i do wrong?
P. S. I am new in iPhone sdk, so don't judge me
You never set the frame for the UIImageView. You need to offset the x coordinate of the origin of the frame (frame.origin.x) by width of the image view + padding.
Your forgot to set the frame for your imageView to with the x-Offset of xAxis.

Make UITableView imageView square?

I have a app that will populate a UITableView after the user takes a picture.
The UITableViewCell's image is that picture that the user takes. I would prefer these images to be square.
I have tried just about everything, here's some code:
UIImage *theImage = [UIImage imageNamed:theImagePath];
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.image = theImage;
Thanks!
Coulton
one option would be to pass in a square image:
UIGraphicsBeginImageContext(CGSizeMake(60.0f, 60.0f));
[fullSizeImage drawInRect:CGRectMake(0,0,60.0f, 60.0f)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
you would probably want to only do this once for each image, and then cache the result.