【问题标题】:Retrieving image from asset folder - iOS从资产文件夹中检索图像 - iOS
【发布时间】:2017-05-06 12:15:05
【问题描述】:

我正在编写一个类来构建一个 cocoapods,我想获取我放在 Assets.xcassets 中的图像。问题总是得到nil 图像。有什么我想念的吗?

这是我的podspec 文件:

目录结构如下:

这是我尝试加载的图像:

#import "HaloImage.h"

@implementation HaloImage
- (void)showImage {
    NSBundle * bundle = [NSBundle bundleForClass:self];
    NSURL * bundleUrl = [bundle URLForResource:@"Assets" withExtension:@"bundle"];
    self.image = [UIImage imageNamed:bundleUrl.absoluteString];
}
@end

【问题讨论】:

  • 我相信只要将资产包含在项目文件中,您应该可以调用其中的所有资产
  • 我也是,但实际上我无法显示我想要的图像。

标签: ios objective-c iphone cocoapods cocoapods-app


【解决方案1】:

您可以通过这个访问 Assets.xcassets 中的图像。试试看

UIImage *image =  [UIImage imageNamed:@"image name without extension"];

【讨论】:

    【解决方案2】:

    你能试试下面的代码吗:

    @implementation HaloImage
    - (void)showImage {
        NSBundle * bundle = [NSBundle bundleForClass:self];
        NSURL * bundleUrl = [bundle pathForResource:@"Assets" withExtension:@"bundle"];
        self.image = [UIImage imageNamed:bundleUrl.absoluteString];
    }
    @end
    

    【讨论】:

      【解决方案3】:

      您只能按名称(png 文件)使用资产或主包中的图像

      self.image = [UIImage imageNamed:@"name_of_image"];
      

      【讨论】:

      • 不幸的是它不起作用,我实际上尝试过该代码。
      猜你喜欢
      • 2012-05-22
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多