如何从ios5中的iphone上发送图像

时间:2011-12-01 04:58:46

标签: objective-c xcode ios4

我想使用twitter api twitter + OAuth在我的iphone上分享图片,但我不知道如何从我的iphone分享。我使用sharekit尝试了相同的操作,但我没有成功。你能告诉我如何使用twitter api在iphone上分享图片吗?

3 个答案:

答案 0 :(得分:5)

zot提供的链接有效但我必须添加一些代码加上我必须添加两个框架Twitter.framework和Account.framework。修改后的代码如下

- (IBAction)sendCustomTweet:(id)sender {
    // Create an account store object.
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    // Request access from the user to use their Twitter accounts.
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if(granted) {
            // Get the list of Twitter accounts.
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];


            if ([accountsArray count] > 0) {
                // Grab the initial Twitter account to tweet from.
                ACAccount *twitterAccount = [accountsArray objectAtIndex:0];


                UIImage *image =[UIImage imageNamed:@"Icon.png"];


                TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] 
                                                             parameters:[NSDictionary dictionaryWithObject:@"Hello. This is a tweet." forKey:@"status"] requestMethod:TWRequestMethodPOST];

                   // "http://api.twitter.com/1/statuses/update.json" 

                [postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/png"];


                // Set the account used to post the tweet.
                [postRequest setAccount:twitterAccount];

                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                    [self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
                }];
            }
        }
    }];
}

答案 1 :(得分:1)

你看过这个:iOS 5 Attach photo to Twitter with Twitter API

这仅适用于配备iOS5的手机。

在我的应用程序(iOS5之前)中,我使用TwitPic上传照片并发布Twitter状态。有一个关于如何使用ASHttpRequest在这里进行Twitter状态更新的很好的教程:

http://www.icodeblog.com/2009/07/09/integrating-twitter-into-your-applications/

如果您想要使用此方法,请告诉我,因为除了本教程之外还需要执行其他工作,您还需要从TwitPic获取API密钥。

答案 2 :(得分:0)