简体   繁体   中英

Adding an image to Facebook using social framework

I am getting this error when trying to upload the selected photo for the SLComposeViewController:

[CFNumber retain]: message sent to deallocated instance 0xa1c2c00

The project builds without an errors or warnings. Here is the code:

- (IBAction)test:(id)sender {
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

        API* api = [API sharedInstance];
        NSURL* imageURL = [api urlForImageWithId:IdPhoto isThumb:NO];        
        NSData *data = [NSData dataWithContentsOfURL:imageURL];
        UIImage *img = [[UIImage alloc] initWithData:data];

        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        [controller setInitialText:@"First post from my iPhone app"];
        [controller addURL:[NSURL URLWithString:@"http://www.appcoda.com"]];
        [controller addImage:img];
        [self presentViewController:controller animated:YES completion:Nil];
    }
}

any ideas why? thanks

edit : Here is the viewDidLoad:

-(void)viewDidLoad {

    API* api = [API sharedInstance];
    //load the caption of the selected photo
    [api commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"stream", @"command", IdPhoto,@"IdPhoto",label.text,@"weddingID", nil] onCompletion:^(NSDictionary *json) {
        //show the text in the label
        NSArray* list = [json objectForKey:@"result"];
        NSDictionary* photo = [list objectAtIndex:0];
        lblTitle.text = [photo objectForKey:@"title"];
    }];
    //load the big size photo
    NSURL* imageURL = [api urlForImageWithId:IdPhoto isThumb:NO];
    [photoView setImageWithURL: imageURL];
           NSLog(@"image url :%@",imageURL);

}

First check your imageURL is getting correct or not, There is no problem in SLComposeViewController with ARC.

or you can try if your photoView is imageview

- (IBAction)test:(id)sender {
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        [controller setInitialText:@"First post from my iPhone app"];
        [controller addURL:[NSURL URLWithString:@"http://www.appcoda.com"]];
        [controller addImage: photoView.image];
        [self presentViewController:controller animated:YES completion:Nil];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM