UIDocumentInteractionController和UIActivityViewController都提供了用于将图像共享到其他网络的菜单。点击Instagram后,您会看到一个很好的模态,允许您在不离开应用程序的情况下发布到Instgram。我的问题是,如何在不显示菜单的情况下自动显示该模态?
此应用程序名为Sounds,因此我知道它可行,但我无法在线查找有关其完成方式的任何文档。以下是我显示菜单的当前代码:
NSString *documentDirectory=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *saveImagePath=[documentDirectory stringByAppendingPathComponent:@"Image.ig"];
NSData *imageData=UIImagePNGRepresentation(cardImage);
[imageData writeToFile:saveImagePath atomically:YES];
NSURL *imageURL=[NSURL fileURLWithPath:saveImagePath];
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:imageURL];
docController.UTI = @"com.instagram.exclusivegram";
docController = [self setupControllerWithURL:imageURL usingDelegate:self];
[docController presentOpenInMenuFromRect:CGRectMake(1, 1, 1, 1) inView:self.view animated:YES];
这是菜单:
这是我想要自动显示的内容:
答案 0 :(得分:2)
只要活动项目中只有UIImage,您就可以使用UIActivityViewController来显示Instagram。 如果您添加更多项目,如文本,网址等,它将不会显示。 这是Instagram的一个不好的限制。
像这样:
NSMutableArray *items = [NSMutableArray array];
[items addObject:[UIImage ...]];
// show view
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items
applicationActivities:nil];
[vc presentViewController:activityVC animated:YES completion:nil];
答案 1 :(得分:1)
我用来在Instagram上分享图片的代码
import UIKit
import Photos
class SocialShare: NSObject {
static let shared = SocialShare()
func postImageToInstagram(image: UIImage) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(SocialShare.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
if error != nil {
print(error)
}
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions)
if let lastAsset = fetchResult.firstObject as? PHAsset {
let localIdentifier = lastAsset.localIdentifier
let u = "instagram://library?LocalIdentifier=" + localIdentifier
UIApplication.sharedApplication().openURL(NSURL(string: u)!)
}
}
}
没有任何地方你只想打电话
SocialShare.shared.postImageToInstagram(UIImage(named: "1"))
答案 2 :(得分:1)
这是一个实现Zuhair答案的Swift 4类。
import Foundation
import Photos
class InstagramSharer: NSObject {
static let shared = InstagramSharer()
func post(image: UIImage) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
}
@objc
private func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
guard error == nil else {
return
}
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions)
if let lastAsset = fetchResult.firstObject {
let localIdentifier = lastAsset.localIdentifier
let url = "instagram://library?LocalIdentifier=" + localIdentifier
UIApplication.shared.openURL(URL(string: url)!)
}
}
}
用法
InstagramSharer.shared.post(image: #YOUR_IMAGE#)
答案 3 :(得分:0)
要删除其他应用程序,请更改以下代码行(只需翻转它们):
docController.UTI = @"com.instagram.exclusivegram";
docController = [self setupControllerWithURL:imageURL usingDelegate:self];
这样在覆盖docController对象后设置.UTI变量。
编辑:如果您尝试直接打开Instagram应用程序并填写完信息并准备共享,请查看iPhone挂钩here。