从ViewController设置自定义UIView的图像

时间:2014-08-08 03:08:18

标签: ios objective-c uiview uiimageview xib

我正在尝试创建一个这样的应用程序:

http://topmobiletrends.com/wp-content/uploads/2013/10/screen568x568-14.jpeg

其中每个视图都显示JSON数组中照片中每个视图的图像。

我已成功设置并下载了图片数组,并将其设置为UIImageViewNSURLRequestSDWebImage

我创建了一个带有图片视图的UIView来测试我ViewController(Storyboard)中的代码,一切正常。但是,当我尝试以编程方式创建UIView时,它显示为空白,没有ImageView。我不确定我做错了什么。

以下是我的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];

   }

2 个答案:

答案 0 :(得分:0)

试试这个。主要问题是你没有为图像视图设置框架

 for (NSObject *photo in photos) {
    //UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(160,250,258,229)];
    //dView.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(160,250,258,229)];
    [self.myImage addSubview:imageView];

    priceLabel.text = [[[jsonDictionary objectForKey:@"site"] objectAtIndex:index] objectForKey:@"price"];
    //[imageView addSubview:dView];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"URL"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (imgData) {
                [imageView setImage:[UIImage imageWithData:imgData]];
            }
        });
    });
    }

答案 1 :(得分:0)

您要将dView添加为图片视图的子视图,可能是[dView addSubview:imageView];