zoukankan      html  css  js  c++  java
  • 从主文件包加载数据

    问题:

      你已经在 Xcode 中添加了一些资源(例如图片),现在,在运行时,你希望访问这些资源。 

    方法:

      使用 NSBundle 类中的 mainBundle 类方法来访问主文件包。一旦这个步骤完成,使用 pathForRecource: ofType:方法从主文件包中获得具体资源的路径,路径明确之后,根据资 源的类别可以获取具体文件,比如 UIImage 或者 NSData,或者通过 NSFileManager 手动获取文件。

    例子:

      找到指定文件做一些处理

    [[NSBundle mainBundle] pathForResource:@"AlanSugar"
    ofT ype:@"png"];
    if ([alanSugarFilePath length] > 0){
    UIImage *image = [UIImage imageWithContentsOfFile:alanSugarFilePath];
    if (image != nil){
    NSLog(@"Successfully loaded the file as an image.");
    } else {
    NSLog(@"Failed to load the file as an image.");
    }
    } else {
    NSLog(@"Could not find this file in the main bundle.");
    }
    //找到指定文件存入内存
    [[NSBundle mainBundle] pathForResource:@"AlanSugar" ofT ype:@"png"]; if ([alanSugarFilePath length] > 0){ NSError *readError = nil; NSData *dataForFile = [[NSData alloc] initWithContentsOfFile:alanSugarFilePath options:NSMappedRead error:&readError]; if (readError == nil && dataForFile != nil){ NSLog(@"Successfully loaded the data."); } else if (readError == nil && dataForFile == nil){ NSLog(@"No data could be loaded."); } else { NSLog(@"An error occured while loading data. Error = %@", readError); } } else { NSLog(@"Could not find this file in the main bundle."); }
  • 相关阅读:
    Jetson AGX Xavier配置使用OpenCV(报错:No package 'opencv' found)
    centos7 lnmp
    php用户签到,领取红包
    php红包功能
    Mysql 主从复制
    mysql 小技巧
    区块链学习笔记一
    PHP 常用自定义函数
    centos7.4 搭建lnmp
    什么是跨域?怎么解决跨域问题?
  • 原文地址:https://www.cnblogs.com/safiri/p/4010936.html
Copyright © 2011-2022 走看看