How to Get Image position in ImageView - iphone

I use a full screen imageView to display the image, as follows:
UIImageView*imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)]
imageView.contentMode=UIViewContentModeScaleAspectFit;
imageView.image=srcImage;
As the srcImage size differs, its position (frame) in the imageView differs, how can I
get the position of the image in imageView?
thanks a lot.

You have to do the math yourself. Calculate the aspect ratio of your image and compare with the aspect ratio of the image view's bounds.

Related

Incorrect UITableViewCell height UIImageView

I have a UITableViewCell that only contains a UIImageView. I am using UITableViewAutomaticDimension which is working perfectly for all the other cells in my tableview, except for this image view cell.
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
I want the image to be as tall as it wants, maintain the image's aspect ratio and fill the width. To do that I set the image view's constraints to be pinned to the cell's frame using auto layout and I set the image views contentMode to be Aspect Fit. I set the UIImageView's background to be red. And the cell above it is white. The actual image is black down below.
When I do that, the cell has about 100 px above the image and 100px below. but the images size is exactly what I want. Has anyone had the same issue?
Because that red space isn't always the same size and it was a lot taller when I have a really big image in there, I believe the layout thinks the height should be the actual height of the image, without any aspect fit constraint.
Thanks for any help!
I want the image to be as tall as it wants, maintain the image's aspect ratio and fill the width. To do that I set the image view's constraints to be pinned to the cell's frame using auto layout and I set the image views contentMode to be Aspect Fit.
You can't do all that with only auto layout. This is because a UIImageView's intrinsic content size is just its image's full size.
Instead, calculate the appropriate height and set it when you set your image:
let imageViewWidth : CGFloat = // the width you want…
imageHeightConstraint.constant = image.size.height / image.size.width * imageViewWidth;
Note that this will fail if you have a 0x0 image due to division by zero. If that's possible in your app, check for this case and just set the height constant to 0.
At this point, reload the row so that the table view will recalculate the height.

UIImageView content pixels (ingore image padding)

The short version: How do I know what region of a UIImageView contains the image, and not aspect ratio padding?
The longer version:
I have a UIImageView of fixed size as pictured:
I am loading photos into this UIViewController, and I want to retain the original photo's aspect ratio so I set the contentMode to Aspect Fit. This ends up ensuring that the entire photo is displayed within the UIImageView, but with the side effect of adding some padding (configured in red):
No problem so far.... But now I am doing face detection on the original image. The face detection code returns a list of CGRects which I then render on top of the UIImageView (I have a subclassed UIView and then laid out an instance in IB which is the same size and offset as the UIImageView).
This approach works great when then photo is not padded out to fit into UIImageView. However if there is padding, it introduces some skew as seen here in green:
I need to take the image padding into account when rendering the boxes, but I do not see a way to retrieve it.
Since I know the original image size and the UIImageView size, I can do some algebra to calculate where the padding should be. However it seems like there is probably a way to retrieve this information, and I am overlooking it.
I do not use image views often so this may not be the best solution. But since no one else has answered the question I figured I'd through out a simple mathematical solution that should solve your problem:
UIImage *selectedImage; // the image you want to display
UIImageView *imageView; // the imageview to hold the selectedImage
NSInteger heightOfView = imageView.frame.size.height;
NSInteger heightOfPicture = selectedImage.size.height;
NSInteger yStartingLocationForGreenSquare; // set it to whatever the current location is
// take whatever you had it set to and add the value of the top padding
yStartingLocationForGreenSquare += (heightOfView - heightOfPicture) / 2;
So although there may be other solutions this is a pretty simple math formula to accomplish what you need. Hope it helps.

Distorted content when scaling UIImageView

I have a UIImageView with a frame of (0, 0, 568, 300) containing a UIImage with a native size of 512x384 pixels. The contentmode is set to Aspect fit.
If the user double-Taps on the view I change the size of the UIImageView with the following code:
self.imageViewerViewController.imageView.frame = CGRectMake(0, -63, 568, 426);
the result is that the right edge of the image is distorted, it does not properly scale to the new size.
Attached is an image with a black and white matrix, the distortion is on the right.
It seems that the right column of pixels is repeated to the right edge of the view.
Can anyone help?
I have changed the creation of the affected UIImageView from a xib file to creating it in code and using the method - (id)initWithFrame:(CGRect)aRect: with the maximum size of the scaled image for aRect. Now the image is properly scaled.
Obviously the UIImageView in the xib file has been inited with a 512x384 pixel frame.

CGAffineTransformScale scales imageview,but its sub elements on redrawing got blurred

I am simply using this code to transform imageView to scale double or triple.
imageView.transform = CGAffineTransformScale(CGAffineTransformIdentity, scale, scale);
I am drawing lines on this imageView in drawRect,after scaling everything got scaled. What i want is those lines which i am drawing should remain of same size or width after scaling.In order to do that ,i am redrawing lines on scaled imageView with proportional less height and width but result is like lines got blurred/shady.I think on transforming the pixels size also got increase of imageView..I want to know is there any way to draw proper lines without any blurred effect on transformed imageView etc..
Draw the lines AFTER you scale the image. Just change the width of the line to be the same scale (ie image is 4x, line width is 4x).

How to specify the image location based on left and top values

I am developing an application in which i have one image and I need to specify the location in image.But i have left and top values of image only. But we need x,y,width and height values.Then how to find out values from these.So please tell me how to do that.
The left and top values are the x - y positions. The amount of space your image needs to cover the view (frame size) is the width and height. For example, if your image has to cover the full screen of iPhone, you should specify the frame as:
image.frame = CGRectMake(0, 0, 320, 480);