swift post gif animation to twitter
问题描述:
使用swift语言,我试图发布一个创建的gif动画到twitter,但是界面只支持addImage - 发布普通图片。当我保存的GIF图片,使用swift post gif animation to twitter
let data = NSData(contentsOfURL: url)
var library : ALAssetsLibrary = ALAssetsLibrary()
library.writeImageDataToSavedPhotosAlbum(data, metadata: nil, completionBlock:{
(assetURL: NSURL!, error: NSError!) -> Void in
})
它工作正常,人们可以从照片上传gif动画,但是,没有在Twitter的界面没有这样的事情,从应用程序发布。
有像创建animatedimagewithimages一些建议:
let im = UIImage.animatedImageWithImages(imga, duration: 0.1)
tweetSheet.addImage(im)
但ID也不管用,发布静态图像。
有没有其他的选择?
答
Twitter现在支持GIF动画,所以不知道你是否已经解决了你的问题。我用动画Gif测试了这段代码。
func tweetWithImage(data:NSData)
{
let account = ACAccountStore()
let accountType = account.accountTypeWithAccountTypeIdentifier(
ACAccountTypeIdentifierTwitter)
account.requestAccessToAccountsWithType(accountType, options: nil,
completion: {(success: Bool, error: NSError!) -> Void in
if success {
let arrayOfAccounts =
account.accountsWithAccountType(accountType)
if arrayOfAccounts.count > 0 {
let twitterAccount = arrayOfAccounts.first as! ACAccount
var message = Dictionary<String, AnyObject>()
message["status"] = "Test Tweet with image"
let requestURL = NSURL(string:
"https://api.twitter.com/1.1/statuses/update.json")
let postRequest = SLRequest(forServiceType:
SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
URL: requestURL,
parameters: message)
postRequest.account = twitterAccount
postRequest.addMultipartData(data, withName: "media", type: nil, filename: nil)
postRequest.performRequestWithHandler({
(responseData: NSData!,
urlResponse: NSHTTPURLResponse!,
error: NSError!) -> Void in
if let err = error {
println("Error : \(err.localizedDescription)")
}
println("Twitter HTTP response \(urlResponse.statusCode)")
})
}
}
else
{
// do what you want here
}
})
}
我已经把一个教程在我的博客在GitHub上的相应的项目,说明使用SLRequest使GIF动画的支持...你可以看到它在这里:http://www.iosinsight.com/twitter-integration-with-swift/
注:博客代码并且此答案中的片段使用“update_with_media”更新为请求URL的简单“更新”,因为后者在Twitter API中已弃用。