Christian Seiler
Christian Seiler

Reputation: 1140

Insert UIImage into ScrollView in Code

I pass some data through a segue (name, description and name of photo of a room)

I now want to set the image into a scrollView so that the user can scroll the panorama pictures.

I get the correct URL of the image, but I don't get the image into the scrollView.

My code is this:

    #import "RoomsDetailVC.h"
    #import "RoomsViewController.h"

    @interface RoomsDetailVC ()

    @property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (strong, nonatomic) UIImageView *imageView;

    @end

    @implementation RoomsDetailVC

    @synthesize roomName;
    @synthesize priceLabel;
    @synthesize descriptionLabel;
    @synthesize roomImageString;

    - (void)viewDidLoad {
        [super viewDidLoad];

        self.roomName.title = [self.roomNameString description];
        self.priceLabel.text = [self.priceLabelString description];
        self.descriptionLabel.text = [self.descriptionLabelString description];

        [self.scrollView addSubview:_imageView];
        [self setImage];
    }

    - (void)setImage {
        if (self.scrollView) {
            NSString *imageName = [self.roomImageString description];
            NSURL *imageURL = [[NSBundle mainBundle] URLForResource:imageName withExtension:@"png"];
            NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];


            UIImage *image = [[UIImage alloc] initWithData:imageData];
            if (image) {
                self.scrollView.zoomScale = 1.0;
                self.scrollView.contentSize = image.size;
                self.imageView.image = image;
                self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
                NSLog(@"image: %@", imageURL);
            } else {
                NSLog(@"no image");
            }
        }
    }

    - (UIImageView *)imageView {
        if (!_imageView) _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        return _imageView;
        NSLog(@"imageView");
    }

    @end

The NSLog result of self.scrollView.description is this:

scrollView: <UIScrollView: 0x1c114410; frame = (20 268; 280 160); clipsToBounds = YES; autoresize = RM+TM; gestureRecognizers = <NSArray: 0x1c114a10>; layer = <CALayer: 0x1c114660>; contentOffset: {0, 0}>

The NSLog result of `self.imageView.description is this:

imageView: <UIImageView: 0x1c115e90; frame = (0 0; 516 160); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1c113160>>

Upvotes: 0

Views: 151

Answers (2)

Vik
Vik

Reputation: 1927

I'm afraid when you add _imageView to the scroll view it's actually nil. You should use self.imageView or maybe better add the imageView to the scrollView after downloading it

Upvotes: 0

John Riselvato
John Riselvato

Reputation: 12904

i think it's because you are adding the subview (_imageView) before even getting the image.

reverse this:

    [self.scrollView addSubview:_imageView];
    [self setImage];

to

    [self setImage];
    [self.scrollView addSubview:_imageView];

Upvotes: 2

Related Questions