ios - 单例类保留 'Losing' UIImage

标签 ios objective-c singleton

我有一个 Singleton 类,我试图用它来存储从 Internet 下载的图像,以便我可以随时访问它。工作流程是我登录到 Facebook,并将图像下载到 Singleton 类。一切都很好,它显示图像也很好。但是,如果我停止运行该应用程序,图片将不再存在。有人可以看看我的代码,看看我是否遗漏了一些东西来让它始终保持图像吗?就像我之前说的,第一次,它运行良好,但如果我退出应用程序,UIImage 就不再存在了。

FBSingleton.h

@interface FBSingleton : NSObject

@property (nonatomic, strong) UIImage *userImage;

+ (instancetype)sharedInstance;
@end

FBSingleton.m

@implementation FBSingleton

FBSingleton *sharedInstance = nil;

// Get the shared instance and create it if necessary.

+ (FBSingleton *)sharedInstance {
    @synchronized(self){
        if (sharedInstance == nil) {
            sharedInstance = [[super allocWithZone:NULL] init];
        }
    }
    return sharedInstance;
}

- (id)init
{
    self = [super init];

    if (self) {
        // Work your initialising magic here as you normally would

        self.userImage = [[UIImage alloc] init];
    }

    return self;
}

@end

ViewController(负责保存图片)

NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=square&return_ssl_resources=1", facebookID]];

                NSLog(@"Picture URL%@", pictureURL);
                NSURLRequest *urlRequest = [NSURLRequest requestWithURL:pictureURL];

                // Run network request asynchronously
                [NSURLConnection sendAsynchronousRequest:urlRequest
                                                   queue:[NSOperationQueue mainQueue]
                                       completionHandler:
                 ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                     if (connectionError == nil && data != nil) {
                         // Set the image in the header imageView
                         //  self.headerImageView.image = [UIImage imageWithData:data];
                        FBSingleton *sharedSingleton = [FBSingleton sharedInstance];
                         UIImage *image = [UIImage imageWithData:data];
                         sharedSingleton.userImage = image;
                     }
                 }];

最佳答案

您似乎希望将图像保存到磁盘,但我没有看到任何将图像保存到磁盘或从磁盘加载图像的代码。您需要将图像写出到一个文件中,以便在应用程序运行之间保留它; strong 属性中保存的图像仅在分配内存时保存。当您的应用程序退出时,内存将被释放。

很难说这是否确实是问题所在,但根据您对遇到的问题的描述,这很可能是您所看到的“错误”的原因。尝试使用以下之一:

[UIImagePNGRepresentation(image) writeToFile:cachedNameAbsolutePath atomically:YES];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:cachedNameAbsolutePath atomically:YES];

然后当您的应用启动时,检查 cachedNameAbsolutePath 是否存在并加载图像。如果它不存在,那就是您联系 Facebook 重新下载该图像的时候。

关于ios - 单例类保留 'Losing' UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27802759/

相关文章:

ios - EasyMapping 库 : can i get data for 1 object from different dictionary

objective-c - 在 UIImage 中编辑颜色字节

iphone - 如何在 Objective-C 中创建单例类

单例模式下的Java同步

c# - .NET 双重检查锁定中对 volatile 修饰符的需求

ios - CFSocket数据回调

ios - 按需重新加载 UITabBarController

ios - 有没有办法通过按钮操作将用户发送到设置中的推送通知部分?

objective-c - 对于 Cocoa 应用程序,哪些文件应该受到版本控制?

objective-c - UIBezierPath containsPoint : don't work correctly?(更新: touch location in superview how to handle?)