【问题标题】:Swift 2 get the number of images in main bundle [closed]Swift 2 获取主包中的图像数量 [关闭]
【发布时间】:2015-11-22 15:14:44
【问题描述】:

如何将它从 obj-c 转换为 swift 2?

myArray = [[NSMutableArray alloc] init];
    UIImage *image;
    NSUInteger nimages = 0; 
    for (; ; nimages++)
    {
        NSString *nameOfImg_ = entity.attribute;
        NSString *imageName = [NSString stringWithFormat:@"name%@number%lu.jpg", nameOfImg_, (unsigned long)(nimages + 1)];
        image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imageName ofType:nil]];
        if (image == nil) {
            break;
        }
        [myArray addObject:image];
    }

【问题讨论】:

  • 这不是免费的代码翻译服务。做一次尝试。用您尝试的 Swift 代码更新您的问题。说明您遇到了什么问题。
  • @rmaddy 没有人要求你投稿。如果您不想贡献,请保留给自己。
  • 我告诉你这个网站是如何运作的。这是一个有效的贡献。
  • @rmaddy 是“障碍物”。但我们不要为此争论。

标签: ios swift2


【解决方案1】:

试试这个

已编辑

let myArray = NSMutableArray()
    var image:UIImage!
    var nimages = 0

    for(;;nimages++){

        let nameOfImg_ = entity.attribute
        let imageName = String(format: "name%@number%lu.jpg", arguments: [nameOfImg_,(nimages + 1)])
        if((NSBundle.mainBundle().pathForResource(imageName, ofType: nil)) != nil){
            image = UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource(imageName, ofType: nil)!)
            myArray.addObject(image!)

        }else{
            break
        }
    }

entity.attribute 必须是字符串

【讨论】:

  • 我将进入永恒循环添加图像永远......似乎 break 不起作用!
  • 从 break.well 中删除分号,这并没有多大意义。也告诉我你想达到什么目标。
  • 同样的事情...无休止的循环...我有有限数量的图像(从 1 到 10 不等),我想将这些添加到 myArray。如果图像到达最后一张图像,则中断...并停止
  • 我怀疑image = UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource(imageName, ofType: nil)!) 这行代码会崩溃
  • @GeorgeAsda 将 %s 更改为 %@ name%@number%lu.jpg。这不是解决方案。只是一个错误修复。我也更新了
【解决方案2】:
var myArray = [UIImage]()

var image : UIImage? = nil
var nimages = 0

while nimages != 0  && image == nil{
    let imageName = "name\(entity.attribute)number\(nimages+1)"
    image = UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource(imageName, ofType: nil )!)
    nimages++
    if image != nil{
        myArray.append(image!)
    }
}

【讨论】:

    【解决方案3】:

    如果问题是要获取主包中包含entity.attribute 字符串的所有图像,则这是一个“本机”翻译。

    let imagePaths = NSBundle.mainBundle().pathsForResourcesOfType("jpg", inDirectory:  nil)
    let myArray = imagePaths.filter{ $0.containsString(entity.attribute) }.map { UIImage(contentsOfFile: $0) }
    

    【讨论】:

      【解决方案4】:
      var myArray = [UIImage]()
      var image:UIImage?
      var nimages: UInt = 0
      for ; ; nimages++ {
          var nameOfImg_: String = entity.attribute
          var imageName: String = "name\(nameOfImg_)\((nimages + 1)).jpg"
          image = UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource(imageName, ofType: nil))
          if image != nil {
              myArray.append(image)
          }
      }
      

      【讨论】:

      • 当您将数组声明为var myArray = [UIImage]() 时,没有什么类似于addObject 的方法。应该是append
      • @RajanMaheshwari Thx,已修复
      猜你喜欢
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 2015-10-06
      • 2023-04-04
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      相关资源
      最近更新 更多