1

我正在尝试使用 TWRequest 对象发布图像。这是我的代码:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSData *pngImage = UIImagePNGRepresentation(image);
    NSData *tweetText = [userText copy];
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if (granted) {
            NSLog(@"Twitter is good to go");
            NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
            if (twitterAccounts.count) {
                ACAccount *twitterAccount = [twitterAccounts objectAtIndex:0];
                NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"];
                TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:nil requestMethod:TWRequestMethodPOST];
                [request addMultiPartData:pngImage withName:@"media" type:@"image/png"];
                [request addMultiPartData:tweetText withName:@"text" type:@"text/plain"];
                [request setAccount:twitterAccount];
                [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    if (responseData)
                    {
                        NSError *error = nil;
                        NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
                        if (jsonArray) {
                            for(NSDictionary *item in jsonArray) {
                                NSLog(@"Item: %@", item);
                            }
                        }
                        else
                        {
                            NSLog(@"Error %@ with user info %@.", error, error.userInfo);
                        }
                    }
                }];                                   
            }
        } else {
            NSLog(@"The user does not grant us permission to access its Twitter account(s).");
        }
    }];

每当程序到达行时,[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {我都会收到以下错误:

[__NSCFString 字节]:无法识别的选择器发送到实例 0x1d8f4250 * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString 字节]:无法识别的选择器发送到实例 0x1d8f4250”*第一次抛出调用堆栈:(0x33ec02a3 0x3bb5a97f 0x33ec38107 03e19fx26 0x3473B90B 0x35C416FD 0x35C417A3 0x35C4065F 0x35C3FA6B 0X35C40AB9 0X5ADC5 0X3BF72BF72BF72BF72BF72BF7BF7BF7FDCBBF7FDCB 0x3BF80259BBF803BBF80BBR9BBR9BBFAB9BBFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BFAB9BNBNBER 9.

4

1 回答 1

7

问题出在tweetText. 您正在复制一个NSString但将其分配给一个NSData对象。

改变这个:

NSData *tweetText = [userText copy];

对此:

NSData *tweetText = [userText dataUsingEncoding:NSUTF8StringEncoding];
于 2013-03-20T15:35:35.787 回答