UIImage简谈:[UIImage imageNamed:]与 [UIImage imageWithContentsOfFile]的区别

本文深入探讨了iOS与Android开发中的关键图像处理技术,包括OpenGL ES滤镜、OpenCV图像处理等,提供了针对不同应用场景的高效解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[UIImage imageNamed:]只适合与UI界面中小的贴图的读取,而一些比较大的资源文件应该尽量避免使用这个接口。

直接读取文件路径[UIImage imageWithContentsOfFile]来解决图片的读取问题

这两种方式的主要区别在于:

imageName的方式会在使用的时候系统会cache,程序员是无法处理cache的,这是由系统自动处理的,对于重复加载的图像,速度会提升很多,这样反而用户体验好。所以如果某张图片需要在应用中使用多次,或者重复引用,使用imageName的方式会更好

imageWithContentsOfFile的方式,在使用完成之后系统会释放,不会缓存下来,所以也就没有这样的问题。一般也不会把所有的图片都会缓存。有些图片在应用中只使用一两次的,就可以用这样的方式,比如新手引导界面的图片等等,就适合这样的方式。没有明显的界限。

imageWithContentsOfFile生成的UIImage会随着path的文件内容改变而改变,

比如你从相册中选择n张photo存入数组,如果你将得到的jpg命名为相同的xx.jpg, 然后用imageWithContentsOfFile存入数组,你的数组中的UIImage会不断改变。


CGImageRef转换成UIImage

 CGImageRef iOffscreen =CGBitmapContextCreateImage(context);

UIImage* image = [UIImage imageWithCGImage: iOffscreen];

 

 UIImage转换成CGImageRef

UIImage *loadImage=[UIImage imageNamed:@"comicsplash.png"];

CGImageRef cgimage=loadImage.CGImage;


CGImageRef CGImageCreate(size_t width, size_t height,

    size_t bitsPerComponent, size_t bitsPerPixel, size_t bytesPerRow,

    CGColorSpaceRef space, CGBitmapInfo bitmapInfo, CGDataProviderRef provider,

    const CGFloat decode[], bool shouldInterpolate,

    CGColorRenderingIntent intent);

通过这个方法,我们可以创建出一个CGImageRef类型的对象,下面分别对参数进行解释:


sizt_t是定义的一个可移植性的单位,在64位机器中为8字节,32位位4字节。


width:图片宽度像素


height:图片高度像素


bitsPerComponent:每个颜色的比特数,例如在rgba-32模式下为8

bitsPerPixel:每个像素的总比特数

bytesPerRow:每一行占用的字节数,注意这里的单位是字节

space:颜色空间模式,例如const CFStringRef kCGColorSpaceGenericRGB 这个函数可以返回一个颜色空间对象。


provider:数据源提供者

decode[]:解码渲染数组

shouldInterpolate:是否抗锯齿

intent:图片相关参数


CGImageRef CGImageMaskCreate(size_t width, size_t height,

    size_t bitsPerComponent, size_t bitsPerPixel, size_t bytesPerRow,

    CGDataProviderRef provider, const CGFloat decode[], bool shouldInterpolate)

这个方法用于创建mask图片图层,可以设置其显示部分与不显示部分达到特殊的效果,参数意义同上。


CGImageRef CGImageCreateCopy(CGImageRef image)

这个方法可以复制一个CGImageRef对象


CGImageRef CGImageCreateWithJPEGDataProvider(CGDataProviderRef

    source, const CGFloat decode[], bool shouldInterpolate,

    CGColorRenderingIntent intent)

通过JPEG数据源获取图像


CGImageRef CGImageCreateWithPNGDataProvider(CGDataProviderRef source,

    const CGFloat decode[], bool shouldInterpolate,

    CGColorRenderingIntent intent)

通过PNG数据源获取图像


CGImageRef CGImageCreateWithImageInRect(CGImageRef image,

    CGRect rect)


截取图像的一个区域重绘图像


CGImageRef CGImageCreateWithMask(CGImageRef image, CGImageRef mask)

截取mask图像的某一区域重绘


CGImageRef CGImageCreateWithMaskingColors(CGImageRef image,

    const CGFloat components[])

通过颜色分量数组创建位图


CGImageRef CGImageCreateCopyWithColorSpace(CGImageRef image,

    CGColorSpaceRef space)

通过颜色空间模式复制位图


CGImageRef CGImageRetain(CGImageRef image)

引用+1


void CGImageRelease(CGImageRef image)

引用-1


bool CGImageIsMask(CGImageRef image)

返回是否为Mask图层


size_t CGImageGetWidth(CGImageRef image)

获取宽度像素


size_t CGImageGetHeight(CGImageRef image)

获取高度像素


下面这些方法分别获取相应属性

size_t CGImageGetBitsPerComponent(CGImageRef image)

size_t CGImageGetBitsPerPixel(CGImageRef image)

size_t CGImageGetBytesPerRow(CGImageRef image)

CGColorSpaceRef CGImageGetColorSpace(CGImageRef image)CG_EXTERN CGImageAlphaInfo CGImageGetAlphaInfo(CGImageRef image)

CGDataProviderRef CGImageGetDataProvider(CGImageRef image)

const CGFloat *CGImageGetDecode(CGImageRef image)

bool CGImageGetShouldInterpolate(CGImageRef image)

CGColorRenderingIntent CGImageGetRenderingIntent(CGImageRef image)

CGBitmapInfo CGImageGetBitmapInfo(CGImageRef image)




/ // HBBaseTabbarViewController.m // wechat // // Created by josh on 2025/3/21. // #import "HBBaseTabbarViewController.h" #import "HBCustomTabBar.h" #import "HBFriendLineViewController.h" #import "HBProfileViewController.h" #import "HBHomeViewController.h" #import "HBContactViewController.h" @interface HBBaseTabbarViewController () @end @implementation HBBaseTabbarViewController - (void)viewDidLoad { [super viewDidLoad]; // 替换默认的 TabBar [self setValue:[[HBCustomTabbar alloc] init] forKey:@"tabBar"]; // 设置子视图控制器 [self setupViewControllers]; } - (void)setupViewControllers { // 创建 4 个子视图控制器 HBHomeViewController *homeVC = [[HBHomeViewController alloc] init]; // homeVC.view.backgroundColor = [UIColor whiteColor]; homeVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home-sel"]]; HBContactViewController *contactVC = [[HBContactViewController alloc] init]; // contactVC.view.backgroundColor = [UIColor whiteColor]; contactVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通讯录" image:[UIImage imageNamed:@"contact"] selectedImage:[UIImage imageNamed:@"tongxunlu-sel"]]; HBFriendLineViewController *frienVC = [[HBFriendLineViewController alloc] init]; frienVC.view.backgroundColor = [UIColor whiteColor]; frienVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"朋友圈" image:[UIImage imageNamed:@"pengyouquan"] selectedImage:[UIImage imageNamed:@"pengyouquan-sel"]]; HBProfileViewController *profileVC = [[HBProfileViewController alloc] init]; profileVC.view.backgroundColor = [UIColor whiteColor]; profileVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"me"] selectedImage:[UIImage imageNamed:@"me-sel"]]; // 将子视图控制器添加到 TabBarController self.viewControllers = @[homeVC, contactVC, frienVC, profileVC]; } 这一串代码的作用
最新发布
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值