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."); }
  • 相关阅读:
    sql FLOAT字段使用like查询
    关于sql--时间范围查询重叠
    关于java时间类型比较
    前端-搜索无结果时,怎么把“暂无数据“显示出来?
    v-for动态赋值给不同的下拉框input
    Java的优先队列PriorityQueue详解
    软件体系架构阅读笔记八
    字符数组和字符串之间的转换
    Java快速输入输出
    软件体系架构阅读笔记七
  • 原文地址:https://www.cnblogs.com/safiri/p/4010936.html
Copyright © 2011-2022 走看看