objective-c - 如何从文件名中查找图像数量?

标签 objective-c ios nsstring

我需要从文件名 exp 中知道我有多少张图像: 我有图像调用: 第一个文件: Splash_10001.jpg 最后一个文件: Splash_10098.jpg 我想然后插入数组..

 for(int i = 1; i <= IMAGE_COUNT; i++)
{
    UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%04d.%@",self.firstImageName,i,self.imageType]];
    NSLog(@"%d",i);
    [imgArray addObject:image];
}

我想用数字 98 替换 IMAGE_COUNT,但我需要从用户发送给我的字符串中获取数字:Splash_10098.jpg 我需要将 Splash_10098.jpg 分成: nsstring:Splash_1 int:0098 nsstring:jpg 全部 10 倍!

最佳答案

这取决于授予的字符串输入是什么。接下来,我将搜索点并向后查找最大数字。 顺便说一句,我只能建议使用多语言 NumberFormatter,而不是依赖默认转换。

  NSString * input = @"Splash_19001.jpg";
  NSRange r = [input rangeOfString:@"."];
  if(r.location>4){
    NSString * numberPart = [input substringWithRange: NSMakeRange(r.location-4,4)];
    NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
    [nf setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber * number = [nf numberFromString:numberPart];
    int val = [number intValue];
    NSLog(@"intValue=%d",val);
  }

关于objective-c - 如何从文件名中查找图像数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13107274/

相关文章:

iphone - 如何从地址簿 ios 以编程方式编辑电话号码值

ios - 您的帐户都不是成员(member),升级到 Xcode 8 后出现代码签名错误

ios - UserDefaults 初始值设定项中 suiteName 的真正含义是什么?

ios - 从后台激活 iOS 应用程序的快速机制

iphone - 字符串操作

ios - 将 NSInteger 乘以任何数字时的无效操作数

ios - 如何提高登录认证的速度?

iOS AVPlayerViewController 不显示播放控件

ios - 在 View Controller 之间传递数据 - iOS Xcode - 什么是好方法?

iphone - 如何可靠地将未知编码的文本文件拉入 iPhone 上的 NSString 中?