Search code examples
iosobjective-cuiviewuiimageviewxib

Set Image of Custom UIView from ViewController


I am trying to create an app like this:


(source: topmobiletrends.com)

where each of the views show an image for every in a photo in a JSON array.

I have successfully set and downloaded the array of images and set them in a UIImageView with with NSURLRequest and SDWebImage.

I created a UIView with an image view inside to test the code in my ViewController(Storyboard), everything works fine. However, when I try to create a UIView to loop programmatically, it shows up blank, with no ImageView. I'm not sure what I'm doing wrong.

Here is my code for my ViewController.m

for (NSObject *photo in photos) {

     UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(160,250,258,229)];
        dView.backgroundColor = [UIColor whiteColor];

    dispatch_async(dispatch_get_main_queue(),^{
        UIImage *image = [UIImage imageWithData:data];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        myImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

        //Get image from url
        [self.imageView setImageWithURL:imageURL];
        [self.myImage addSubview:imageView];

        priceLabel.text = [[[jsonDictionary objectForKey:@"site"] objectAtIndex:index] objectForKey:@"price"];
        [imageView addSubview:dView];

    });
        }

        }];

[task resume];

   }

Solution

  • you are adding dView as subview for your image view may be it is [dView addSubview:imageView];