gpt4 book ai didi

ios - 重写 NIB 内 UIImageView 的 initWithImage

转载 作者:行者123 更新时间:2023-11-29 04:34:39 24 4
gpt4 key购买 nike

事情是这样的:我的 Storyboard中有 25 个 View Controller ,每个 View Controller 都有一个 UIImageView,用于显示大文件大小的 PNG。

事实证明, Nib 内的 UIImageView 将使用 [UIImageView initWithImage:[UIImage imageNamed:]] 方法加载图像 (see the docs) 。使用 imageNamed: 的问题是,当加载每个 View Controller 时,其 UIImageView 中的图像被放置在缓存中,并且我的应用程序很快开始收到内存警告并崩溃。

解决办法是使用[UIImageView initWithImage:[UIImage imageWithContentsOfFile:]],它加载图像但不缓存它。所以我想做的是在我的NIB文件中使用调用imageWithContentsOfFile的UIImageView的子类。但我不知道如何获取在 Interface Builder 的属性检查器中设置的图像文件的名称。

UIImageViewNoCache.h

#import <UIKit/UIKit.h>

@interface UIImageViewNoCache : UIImageView
@end

UIImageViewNoCache.h

#import "UIImageViewNoCache.h"

@implementation UIImageViewNoCache

-(id)initWithImage:(UIImage *)image{
self = [super init];
if(self){
self.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], theNameOfTheImageInTheNib];]
}
}
@end

那么,我怎样才能获得theNameOfTheImageInTheNib

最佳答案

看看我写的这个类别:UIImage+SDSCache .

它会调整imageNamed,以便不使用缓存。它可能对您有用,或者作为如何解决您的特定问题的示例。

关于ios - 重写 NIB 内 UIImageView 的 initWithImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11232269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com