1

I'm scaling an image within my UIImageView using : UIViewContentModeScaleAspectFill

The image scales nicely but my issue is the ImageView sits within a UIScrollView, I've set the origin to 0,0. This is fine for the majority of images I'm using but some have a greater height and push out of bounds. I don't want to trim the image, I'd like to dynamically push the UIImageView frame down on the Y Axis based off the amount that's out of bounds.

How can I go about finding out what's out of bounds on the Y Axis / Height? Excuse the poor code example.

NSString *ImageURL = [self.detailItem objectForKey:@"job_header"];

        anImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MAX_HEIGHT, 118)];
        anImage.contentMode = UIViewContentModeScaleAspectFill;
        [anImage setImageWithURL:[NSURL URLWithString:ImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
             //Adjust the frame once the image is loaded
             [self adjustPositions];
        }];

Within adjustPosition:

if (anImage.image.size.height > MAX_HEIGHT) {
    CGRect myFrame = anImage.frame;
    myFrame.origin.y = 100;
    myFrame.origin.y = anImage.frame.size.height / anImage.image.size.height;
    anImage.frame = myFrame;
}

I'm also using: SDWebImage, and this is Q is relevant and may explain my issue a little bit better: UIImageView, setClipsToBounds and how my images are losing their head

Please note that I don't want to clip the bounds at all. I want to preserve bounds and simply move the origin to a true (0,0) position.

Community
  • 1
  • 1
Tom
  • 591
  • 4
  • 20

1 Answers1

0

Here is a method that can calculate the size of the image in your image view. originalSize is the size of the image and constraint is the image view size.

- (CGSize) size: (CGSize) originalSize constrainedToSize: (CGSize) constraint
{
    CGFloat ratioOriginal = originalSize.width/originalSize.height;
    CGFloat ratioConstraint = constraint.width/constraint.height;

    CGSize resultingSize = CGSizeZero;

    if (ratioConstraint >= ratioOriginal) {
            resultingSize = CGSizeMake(constraint.width, originalSize.height*constraint.width/originalSize.width);
    }else{
            resultingSize = CGSizeMake(constraint.height*originalSize.width/originalSize.height, constraint.height);
    }

    return resultingSize;
}

Having the size of the image in the image view you can calculate how much of the image is out of bounds.

Marcin Kuptel
  • 2,674
  • 1
  • 17
  • 22
  • Close but no cigar, the issue I now have is a gap larger than expected. So I've moved the image frame origin.y by the resultingSize.height. I found the gap to be too big, so I thought the bounds might be evenly distributed on the top/bottom. After dividing the result by 2 I'm a little closer, but the image is still eating a small amount into the top. I'm aiming for a 0,0. But obviously the image bounds is causing difficulties. I'm wondering if I should give up and use a proper image scaling lib that won't have anything out of bounds.... – Tom May 10 '13 at 00:17
  • I was able to achieve a reasonable ratio resize whilst still using SDWebImage's async/caching abilities with the following: https://github.com/toptierlabs/ImageCacheResize The only issue I had with this was it's using an older version and isn't as maintained, but it does work. – Tom May 10 '13 at 05:03