Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Singleton不返回任何图像_Ios_Objective C_Facebook - Fatal编程技术网

Ios Singleton不返回任何图像

Ios Singleton不返回任何图像,ios,objective-c,facebook,Ios,Objective C,Facebook,我试图从FB获取配置文件图像,并使用singleton类保存它,但当我尝试在UIImageView中显示它时,它似乎没有保存任何图像。我已经测试了获取facebook个人资料图像的代码,它可以正常工作。为什么使用singleton不起作用 单子 单子 ViewController.h-保存UIImage AnotherViewController.m-获取表单单例类 在您的情况下,当您调用[FBSingleton sharedInstance]时,将不会调用FBSingleton类的init方法

我试图从FB获取配置文件图像,并使用singleton类保存它,但当我尝试在UIImageView中显示它时,它似乎没有保存任何图像。我已经测试了获取facebook个人资料图像的代码,它可以正常工作。为什么使用singleton不起作用

单子 单子 ViewController.h-保存UIImage AnotherViewController.m-获取表单单例类
在您的情况下,当您调用
[FBSingleton sharedInstance]
时,将不会调用
FBSingleton
类的
init
方法,因为在
+sharedInstance
中,您调用了
[super allocWithZone]
,而不是
[self new]即最终版本必须如下所示:

+ (instancetype)sharedInstance
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [self new]; //or [[self alloc] init] which is the same
    });

    return instance;
}

您在singleton类init中错过了
@synchronized(self)

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;
    }
    UIImage *theImage = [[FBSingleton sharedInstance] userImage];

    if (!theImage) {
        // download the image from Facebook and then save it into the singleton



        [FBRequestConnection
         startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSString *facebookId = [result objectForKey:@"id"];

             FBSingleton *sharedSingleton = [FBSingleton sharedInstance];

                UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", facebookId ]]]];

                 sharedSingleton.userImage = image;

             }
         }];




    }
FBSingleton *sharedSingleton = [FBSingleton sharedInstance];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(tableView.frame.size.width/2-75, 50.0f, 100.0f, 100.0f)];
    [imageView setImage:sharedSingleton.userImage];

    [self.view addSubview:imageView];
+ (instancetype)sharedInstance
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [self new]; //or [[self alloc] init] which is the same
    });

    return instance;
}
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;
    }