簡體   English   中英

將圖像設置為多個UIImageViews

[英]Set an image to many UIImageViews

假設我有60-70個UIImageView,並且想同時將動態圖像加載到所有圖像中(可以這么說)。

例如,如果我正在處理一個60-70 div的網頁,並且想給他們全部背景圖像,我可以給他們全部一個myDivs類,然后調用`$('。myDivs')。css('background -image','url('+ imageUrl +')');

我知道如何設置UIImageView的圖像,但是有沒有一種簡單的方法可以在Objective C中一次設置一堆?

我確實嘗試搜索,但到處充斥着很多與之無關的東西。

這取決於您希望顯示imageView的方式。 如果使用UITableView或UICollectionView,則可以在cellForRowAtIndexPath:方法中動態更新放置在單元格中的imageView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...
    [self setCell:cell forIndexPAth:indexPath];

    return cell;
}

- (void)setCell:(UITableViewCell *)cell forIndexPAth:(NSIndexPath *)indexPath
{

     __weak UITableViewCell *weakCell = cell;

    // 5. add picture with AFNetworking
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"www.facebook.com/profileImageLocation"]];
    [cell.profileImage setImageWithURLRequest:request
                                        placeholderImage:nil
                                                 success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                                     weakCell.profileImage
                                                     .image = image;

                                                 } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                                     NSLog(@"bad url? %@", [[request URL] absoluteString]);
                                                 }];
}

另一個選擇是使用這樣的for循環:

- (void)addImagesToAllMyImageViews:(NSArray *)images
{
    for (id obj in images) {

        if ([obj isKindOfClass:[UIImageView class]]) {

            UIImageView *imageView = (UIImageView *)obj;

            imageView.image = [UIImage imageNamed:@"someImage.png"];
        }
    }
}

我認為您可以使用標記屬性,選擇所有ImageView並給它們一個像777和

for(id* subview in self.view.subview) {
   if([subview isKindaOfclass:[UIImageView class]] && (subview.tag == 777)) {
      UIImageView* imageView = (UIImageView*)subview;
      imageVIew.image = youImage;
   }
 }

希望這對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM