ios - 如何在 Swift 中的单个推文中将多个图像上传到推特?
解决方案
我已经使用 TwitterKit 实现了这一点 。在从 Imagepickerview 中选择图像后,单击按钮共享多个图像时调用此函数requestImageToTwitterAccount 。
func requestImageToTwitterAccount(image:UIImage,fileSize:UInt32){
let accountStore = ACAccountStore()
let twitterAccountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)
accountStore.requestAccessToAccounts(with: twitterAccountType, options: nil) { (granted, error) in
if granted {
let accounts = accountStore.accounts(with: twitterAccountType)
if (accounts?.count)! > 0 {
self.twitterAccount = accounts?.last as! ACAccount
self.uploadImageToTwitter(imageURL: image, fileSize: fileSize)
}
else{
let error = "Please set your twitter account in Settings."
print(error)
}
}
else {
print("App permissions are disabled in device twitter settings, please enable it.")
}
}
}
func uploadImageToTwitter(imageURL:UIImage,fileSize: UInt32){
if let imageData : NSData = UIImageJPEGRepresentation(imageURL, 0.5)! as NSData{
self.tweetImageInit(imageData: imageData, imageSize: Int(fileSize))
}else{
print("Something Wrong")
}
}
func tweetImageInit(imageData:NSData,imageSize:Int) {
let uploadURL = NSURL(string:"https://upload.twitter.com/1.1/media/upload.json")
var params = [String:String]()
params["command"] = "INIT"
params["total_bytes"] = String(imageData.length)
params["media_type"] = "image/jpeg"
let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: uploadURL as URL!,
parameters: params)
postRequest?.account = self.twitterAccount;
postRequest?.perform(handler: { ( responseData, urlREsponse,error) in
print(urlREsponse)
if let err = error {
print(error)
}else{
do {
let object = try JSONSerialization.jsonObject(with: responseData! as Data, options: .allowFragments)
print(responseData)
if let dictionary = object as? [String: AnyObject] {
print(dictionary)
if let tweetID = dictionary["media_id_string"] as? String{
self.tweetImageApped(imageData: imageData, imageSize: imageSize, mediaId: tweetID, chunk: 0)
}
}
}
catch {
print(error)
}
}
})
}
func tweetImageApped(imageData:NSData,imageSize:Int ,mediaId:String,chunk:NSInteger) {
let uploadURL = NSURL(string:"https://upload.twitter.com/1.1/media/upload.json")
var params = [String:String]()
params["command"] = "APPEND"
params["media_id"] = mediaId
params["segment_index"] = String(chunk)
let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: uploadURL as URL!,
parameters: params)
postRequest?.account = self.twitterAccount;
postRequest?.addMultipartData(imageData as Data!, withName: "media", type: "image/jpeg", filename:"filename")
postRequest?.perform(handler: { ( responseData, urlREsponse,error) in
print(urlREsponse)
if let err = error {
print(err)
}else{
self.tweetImageFinalize(mediaId: mediaId)
}
})
}
func tweetImageFinalize(mediaId:String) {
let uploadURL = NSURL(string:"https://upload.twitter.com/1.1/media/upload.json")
var params = [String:String]()
params["command"] = "FINALIZE"
params["media_id"] = mediaId
let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: uploadURL as URL!,
parameters: params)
postRequest?.account = self.twitterAccount;
postRequest?.perform(handler: { ( responseData, urlREsponse,error) in
print(urlREsponse)
if let err = error {
print(err)
}else{
do {
self.arrMediaID.append(mediaId)
print(self.arrMediaID)
let object = try JSONSerialization.jsonObject(with: responseData! as Data, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
print(dictionary)
if self.arrMediaID.count == self.arrImage.count
{
self.postStatus(mediaId: self.arrMediaID)
}
}
}
catch {
print(error)
}
}
})
}
func postStatus(mediaId:[String]) {
let uploadURL = NSURL(string:"https://api.twitter.com/1.1/statuses/update.json")
let array = mediaId
let stringRepresentation = array.joined(separator: ",")
print(stringRepresentation)
var params = [String:Any]()
params["command"] = "STATUS"
params["media_ids"] = stringRepresentation
print(params)
let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: uploadURL as URL!,
parameters: params)
postRequest?.account = self.twitterAccount;
postRequest?.perform(handler: { ( responseData, urlREsponse,error) in
print(urlREsponse)
if let err = error {
print(err)
}else{
do {
let object = try JSONSerialization.jsonObject(with: responseData! as Data, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
print(dictionary)
print("video uploaded")
//self.videoURL = nil
self.arrImage.removeAll()
self.arrMediaID.removeAll()
let alert = UIAlertController(title: "Success", message: "video uploaded successfully.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
catch {
print(error)
}
}
})
}
推荐阅读
- caching - 原则 ORM,当每个实体具有嵌套关系时,使用持久化和刷新的批量更新问题(使用缓存的可能性)
- c++ - 我应该如何让用户定义库中的函数?
- scikit-learn - 生成通用句子编码器嵌入维度时出错
- c# - 更改 WPF Groupbox 的颜色和样式
- android - Android导航组件:指向不同活动的深层链接
- python - 使用 AWS CloudWatch 洞察日志的日志检索解决方案
- python - Django 标记/选择列表视图中的一些数据
- python - cv2.getPerspectiveTransform 用于选定区域之外的点
- python - 继续收到 .float64 没有可调用的 rint 属性的错误
- video - 在没有 hwdownload 的情况下将 ffpmeg OpenCL 过滤器输出传递给 NVenc?