LoneWolfPR LoneWolfPR - 6 months ago 30
Objective-C Question

iOS: Align UIImageView to top

I've got a viewcontroller I'm using to load an image from the web. The image is usually going to be much larger than the viewport so I'm scaling it down and placing it in a UIImageView. The problem is it doesn't want to align to the top of the viewport. How would I fix this. Here's the code:

- (void)viewDidLoad
{
NSURL *imageURL = [NSURL URLWithString:[[self exhibit] image]];

NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];

UIImageView *iv = [[UIImageView alloc] initWithImage:image];


[iv setContentMode:UIViewContentModeScaleAspectFit];
[iv setTranslatesAutoresizingMaskIntoConstraints:NO];
[[self view] addSubview:iv];
[self setImageView:iv];

NSDictionary *nameMap = @{@"imageView" : [self imageView]};

NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imageView]-10-|"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:nameMap];
[[self view] addConstraints:horizontalConstraints];

NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageView]"
options:NSLayoutFormatAlignAllTop
metrics:nil
views:nameMap];
[[self view] addConstraints:verticalConstraints];

NSLog(@"%@", NSStringFromCGRect([[self imageView] frame]));
}


The log at the end shows that the UIImageView is the size of the original image size not the scaled down version. I'm assuming this is the problem, but I'm not sure how to fix it.

Answer

I made another solution work. I modified this solution to work for me: What's the easiest way to resize/optimize an image size with the iPhone SDK?

I only need to modify the image if the screenwidth is less than 600px. I also know in advance the aspect ratio of the images I'll be using.

Here's my new viewDidLoad

- (void)viewDidLoad
{
NSURL *imageURL = [NSURL URLWithString:[[self exhibit] image]];

NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];

if ([[UIScreen mainScreen] bounds].size.width < 600 ) {
    CGSize newSize = CGSizeMake(300.0, 140.6);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0.0, 0.0, newSize.width, newSize.height)];
    image = UIGraphicsGetImageFromCurrentImageContext();
}

NSLog(@"%@",NSStringFromCGSize([image size]));

CGRect frame = CGRectMake(0.0, 0.0,image.size.width,image.size.height);
UIImageView *iv = [[UIImageView alloc] initWithFrame:frame];
[iv setImage:image];
[iv setContentMode:UIViewContentModeScaleAspectFit];
[iv setTranslatesAutoresizingMaskIntoConstraints:NO];
[iv setBackgroundColor:[UIColor redColor]];
[[self view] addSubview:iv];
[self setImageView:iv];

[[self imageView] setFrame:CGRectMake(0.0, 0.0, 300.0, 140.6)];


NSDictionary *nameMap = @{@"imageView" : [self imageView]};

NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imageView]-10-|"
                                                                         options:NSLayoutFormatAlignAllCenterX
                                                                         metrics:nil
                                                                           views:nameMap];
[[self view] addConstraints:horizontalConstraints];

NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageView]"
                                                                         options:NSLayoutFormatAlignAllTop
                                                                         metrics:nil
                                                                           views:nameMap];
[[self view] addConstraints:verticalConstraints];
}
Comments