ninja_iOS
ninja_iOS

Reputation: 1223

UIImageView.image setter

I have two side view made this way: containerView - container of both views firstView - visible view which is made of UIImageView secondView - view visible after flipping

Now I'm downloading image async to present on firstView using SDWebImage library with method:

    [view.imageView setImageWithURL:[NSURL URLWithString:item.image]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

Image can be horizontal or vertical. Now I want secondView to be the same size as presented image. I was thinking of setting it in UIImageView custom setter but I don't know how to get that method. Maybe any other ideas?

edit:

Imageenter image description here

I think I know how to do it. What I just need now is actual VISIBLE size of UIImageView.image.

Upvotes: 0

Views: 363

Answers (2)

Ríomhaire
Ríomhaire

Reputation: 3124

Create a property for the image that is being downloaded. Then create a custom setter for this property - in here update your secondImageView size.

This way, each time the imageBeingDownloaded has finished downloading - it will set the image - which will then update the secondImageView size

i.e.

In the implementation file

@property (nonatomic, weak) UIImage *imageBeingDownloaded; // creates the property

// The custom setter
- (void)setImageBeingDownloaded:(UIImage *)imageBeingDownloaded {
    _imageBeingDownloaded = imageBeingDownloaded;
    self.secondImageView.size = _imageBeingDownloaded.size; // each time the image is set this will update secondImageView
}

Then

[view.imageView setImageWithURL:[NSURL URLWithString:item.image] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

becomes

[self.imageBeingDownloaded setImageWithURL:[NSURL URLWithString:item.image] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

Upvotes: 0

Wain
Wain

Reputation: 119031

If you're happy to import AVFoundation to your project these is a convenience function AVMakeRectWithAspectRatioInsideRect which takes an image size and image view frame and gives you the size at which the image will be displayed.

You can obviously write some code yourself to calculate the same values using a little maths.

Upvotes: 1

Related Questions