通过电子邮件共享图像的UIActivityViewController没有扩展名

时间:2013-04-05 04:10:28

标签: ios cocoa-touch

附加的图片只是“附件-1”,没有扩展名。我如何指定一个?

    NSData *compressedImage = UIImageJPEGRepresentation(self.resultImage, 0.8 );

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[ @"Check this out!", compressedImage ] applicationActivities:nil];

    [self.navigationController presentViewController:activityViewController animated:YES completion:nil];

2 个答案:

答案 0 :(得分:14)

根据this answer,您应该能够使用此变通方法指定文件名

NSData *compressedImage = UIImageJPEGRepresentation(self.resultImage, 0.8 );
NSString *docsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imagePath = [docsPath stringByAppendingPathComponent:@"image.jpg"];
NSURL *imageUrl     = [NSURL fileURLWithPath:imagePath];

[compressedImage writeToURL:imageUrl atomically:YES]; // save the file
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[ @"Check this out!", imageUrl ] applicationActivities:nil];

这种方法的明显缺点是您必须将图像保存在磁盘上。

答案 1 :(得分:0)

归功于timeuser的建议,将UIImage转换为NSData。

NSData * data = UIImageJPEGRepresentation(original,1.0);
UIActivityViewController * activity =[[UIActivityViewController alloc] initWithActivityItems:@[@"Your Text", data] applicationActivities:nil];
[self presentViewController:activity animated:true completion:^{ }];