杂技杂记 发表于 2013-1-4 01:34:05

iOS中资源文件的”另类“处理方式

<div id="cnblogs_post_body">早上研究了一下SMCalloutView,发现作者在代码中直接将图片作成了字符串放在了源代码文件中。 这样做的好处是程序依赖的资源文件同code直接放置在了一起,使用的时候只要把源代码拷贝进去就可以了。 要折腾倒也挺简单的:先将图片资源转成Base64字符串,再将字符串声明成常量。使用的时候使用NSData转换一下,再转成UIImage就可以使用了:
<div class="cnblogs_code">+ (UIImage *)embeddedImageNamed:(NSString *)name {    if (.scale == 2)      name = @"$2x"];      SEL selector = NSSelectorFromString(name);      if (![(id)self respondsToSelector:selector]) {      NSLog(@"Could not find an embedded image. Ensure that you've added a category method to UIImage named +%@", name);      return nil;    }      // We need to hush the compiler here - but we know what we're doing!    #pragma clang diagnostic push    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"    NSString *base64String = [(id)self performSelector:selector];    #pragma clang diagnostic pop      UIImage *rawImage = ];    return .scale orientation:UIImageOrientationUp];}
页: [1]
查看完整版本: iOS中资源文件的”另类“处理方式