我正在尝试将其从服务器下载到Instagram后共享图像,这是我用来从服务器下载图像的代码
- (IBAction) DownloadImage:(id)sender {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:sender];
NSData *data = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *viewImage = [[UIImage alloc] initWithData:data];
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)(img));
});
});
}
现在图像已保存到相册中,如何在下载后立即在Instagram应用中打开它?
图像尺寸始终大于612x612px
答案 0 :(得分:0)
过了一会儿,我找到了答案并将其修改为我的代码
@property (nonatomic, retain) UIDocumentInteractionController *dic;
- (IBAction) DownloadImage:(id)sender {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:sender];
NSData *data = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *screenShot = [[UIImage alloc] initWithData:data];
NSString *imagePath = [NSString stringWithFormat:@"%@/image.igo", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
[[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
[UIImagePNGRepresentation(screenShot) writeToFile:imagePath atomically:YES];
_dic = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:imagePath]];
_dic.delegate = self;
_dic.UTI = @"com.instagram.exclusivegram";
NSURL *instagramURL = [NSURL URLWithString:@"instagram://media?id=MEDIA_ID"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[self.dic presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
}
else {
NSLog(@"no instagram app found");
}
});
});
}