首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用twitterkit在twitter上发布图片

使用twitterkit在twitter上发布图片
EN

Stack Overflow用户
提问于 2015-02-23 12:00:50
回答 4查看 4.1K关注 0票数 7

我正在尝试使用具有自定义UI的Twitters新TwitterKit发布图片和推特。他们提供的唯一文档是如何用他们的观点来做这件事。

这样我就可以不需要图像就知道怎么做了

代码语言:javascript
复制
NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[params objectForKey:@"description"],@"status",@"true",@"wrap_links", nil];

NSURLRequest* request = [twAPIClient URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:message error:nil];

[twAPIClient sendTwitterRequest:request completion:^(NSURLResponse* response, NSData* data, NSError* connectionError){



}];

但它们的URLRequestWithMethod方法并不是可变的。我将如何添加一个图像到它。您以前在SLRequest中使用

代码语言:javascript
复制
[postRequest addMultipartData:UIImageJPEGRepresentation(image, 0.5) withName:@"media" type:@"image/jpeg" filename:@"image.png"];
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-02-23 13:28:38

我已经想明白了。

首先,您需要将图片发布到twitter上。

代码语言:javascript
复制
NSString *media = @"https://upload.twitter.com/1.1/media/upload.json";

NSData *imageData = UIImageJPEGRepresentation(image, 0.9);

NSString *imageString = [corgiData base64EncodedStringWithOptions:0];               

NSURLRequest *request = [client URLRequestWithMethod:@"POST" URL:media parameters:@{@"media":imageString} error:&requestError];

[[[Twitter sharedInstance] APIClient] sendTwitterRequest:request completion:^(NSURLResponse *urlResponse, NSData *data, NSError *connectionError) {


}];

然后在response对象中使用media_id_string并将其添加到我问题中的代码参数中。

所以

代码语言:javascript
复制
NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[params objectForKey:@"description"],@"status",@"true",@"wrap_links",mediaIDString, @"media_ids", nil];

NSURLRequest* request = [twAPIClient URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:message error:nil];

[twAPIClient sendTwitterRequest:request completion:^(NSURLResponse* response, NSData* data, NSError* connectionError){

 NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&parsingError];

}];

注意来自第一个请求的响应的media_ids对象

代码语言:javascript
复制
NSMutableDictionary *message = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[params objectForKey:@"description"],@"status",@"true",@"wrap_links",[responseDict objectForKey:@"media_id_string"], @"media_ids", nil];

所以你可以把它放在完成块里面,然后它会发布图片和推特。

票数 7
EN

Stack Overflow用户

发布于 2016-08-30 18:10:34

截至2016年4月,Fabric的TwitterKit 2.0 (或更新版本)推出了一种新的uploadMedia方法来覆盖媒体上传部分。下面是一些适用于我的objc代码。

代码语言:javascript
复制
(earlier)
self.userID = [[Twitter sharedInstance] sessionStore].session.userID;

- (void)tweetImage:(UIImage*)image {
        NSAssert([NSThread currentThread].isMainThread && self.userID, @"Twitterkit needs main thread, with self.userID set");
        if (!self.userID)
            return;
        NSString *tweetStr = @"Look at me! I'm tweeting! #hashtag";

        TWTRAPIClient *twitterClient = [[TWTRAPIClient alloc] initWithUserID:self.userID];
        NSData *imgData = UIImageJPEGRepresentation(image, 0.6f);
        if (!imgData) {
            NSAssert(false, @"ERROR: could not make nsdata out of image");
            return;
        }
        [twitterClient uploadMedia:imgData contentType:@"image/jpeg" completion:^(NSString * _Nullable mediaID, NSError * _Nullable error) {
            if (error) {
                NSAssert(false, @"ERROR: error uploading collage to twitter");
                return;
            }

            NSError *urlerror = nil;
            NSURLRequest *request = [twitterClient URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:@{ @"status":tweetStr, @"media_ids":mediaID } error:&urlerror];
            if (urlerror) {
                NSAssert(false, @"ERROR creating twitter URL request: %@", urlerror);
                return;
            }
            [twitterClient sendTwitterRequest:request completion:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
                if (!connectionError && ((NSHTTPURLResponse*)response).statusCode != 200) {
                    DDLogInfo(@"TwitterHelper tweetImage: non-200 response: %d. Data:\n%@", (int)((NSHTTPURLResponse*)response).statusCode, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
                }
            }];
        }];
    }
票数 3
EN

Stack Overflow用户

发布于 2015-11-29 20:59:48

斯威夫特

代码语言:javascript
复制
func post (tweetString: String, tweetImage: NSData) {

    let uploadUrl = "https://upload.twitter.com/1.1/media/upload.json"
    let updateUrl = "https://api.twitter.com/1.1/statuses/update.json"
    let imageString = tweetImage.base64EncodedStringWithOptions(NSDataBase64EncodingOptions())
    let request = Twitter.sharedInstance().APIClient.URLRequestWithMethod("POST",
        URL: uploadUrl, parameters: ["media": imageString], error: nil)
    Twitter.sharedInstance().APIClient.sendTwitterRequest(request, completion: { (urlResponse, data, connectionError) -> Void in

        if let mediaDict = self.nsdataToJSON(data!) {
            let validTweetString = TweetValidator().validTween(tweetString)
            let message = ["status": validTweetString, "media_ids": mediaDict["media_id_string"]]
            let request = Twitter.sharedInstance().APIClient.URLRequestWithMethod("POST",
                URL: updateUrl, parameters: message, error:nil)

                Twitter.sharedInstance().APIClient.sendTwitterRequest(request, completion: { (response, data, connectionError) -> Void in
            })
        }
    })
}

func nsdataToJSON (data: NSData) -> AnyObject? {
    do {
        return try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
    } catch let myJSONError {
        print(myJSONError)
    }
    return nil
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28673349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档