我最近偶然发现了以下有趣的功能:Instagram iPhone Hooks
我想知道是否能够立即通过documentinteractioncontroller打开应用程序,无需显示预览( - presentPreviewAnimated :)或操作表( - presentOpenInMenuFromRect:inView:animated :).在我看来,这是唯一的方式,但我可能会遗漏一些东西.
-(void) saveToInstagram {
NSURL *url;
UIDocumentInteractionController *dic;
CGRect rect = CGRectMake(0 ,0 , 0, 0);
UIImage *i = imageView.image;
CGRect cropRect = CGRectMake(i.size.width - 306 ,i.size.height - 306 , 612, 612);
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.ig"];
CGImageRef imageRef = CGImageCreateWithImageInRect([imageView.image CGImage], cropRect);
UIImage *img = [[UIImage alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
[UIImageJPEGRepresentation(img, 1.0) writeToFile:jpgPath atomically:YES];
url = [[NSURL alloc] initFileURLWithPath:jpgPath];
dic = [self setupControllerWithURL:url usingDelegate:self];
[dic presentOpenInMenuFromRect:rect inView:self.view animated:YES]; …Run Code Online (Sandbox Code Playgroud) 是否可以绕过行动共享表将照片分享给Instagram?
请注意,我知道的UIDocumentInteractionController和挂钩,事实上它工作正常.通过他们的示例代码,您可以获得一个Copy to Instagram选项(如果您使用独家UTI或可以处理JPG/PNG的大量应用程序,以及Instagram,则可以选择).
这很好,但我想知道是否有办法执行"复制到Instagram"操作,而无需UIDocumentInteractionController在iOS 9+中显示菜单.
为了记录,这是完美运行的代码的简化版本.假设你有一个有效的NSURL ......
guard let data: NSData = NSData(contentsOfURL: url),
image = UIImage(data: data) else {
return
}
let imageData = UIImageJPEGRepresentation(image, 100)
let captionString = "caption"
let writePath = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("instagram.ig")
guard let _ = imageData?.writeToFile(writePath, atomically: true) else {
return
}
let fileURL = NSURL(fileURLWithPath: writePath)
self.documentController = UIDocumentInteractionController(URL: fileURL)
self.documentController.delegate = self
self.documentController.UTI = "com.instagram.photo"
self.documentController.annotation = NSDictionary(object: captionString, forKey: "InstagramCaption") …Run Code Online (Sandbox Code Playgroud)