问题描述
我想在 iOS7 上将图像附加到彩信.我写了以下代码:
I want to attach an image to a MMS, on iOS7. I wrote following code:
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
NSData *imgData = [NSData dataWithContentsOfFile:@"blablabla"];
BOOL didAttachImage = [messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image"];
if (didAttachImage)
{
// Present message view controller on screen
[self presentViewController:messageController animated:YES completion:nil];
}
else
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Failed to attach image"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[warningAlert show];
return;
}
问题是出现短信画面时,无法识别图片,无法发送.我看到的是这样的:
The problem is that when the SMS screen is presented, it doesn't recognize the image, and cannot send it. I see something like this:
我相信这与我发送的 imgData 或 typeIdentifier 有关.
I believe this has something to do either with imgData I am sending, or with typeIdentifier.
注意:我尝试了几乎所有可能的 typeIdentifiers:@"public.data", @"public.image", @"public.item", ... 等等.都没有用.
Note: I tried almost all possible typeIdentifiers:@"public.data", @"public.image", @"public.item", ... etc. None worked.
有人可以帮我吗?您使用的 typeIdentifier 是什么?我正在 iPhone 5、iOS 7.0.2 上进行测试.
Can anybody please help me? What is the typeIdentifier you are using? I am testing on iPhone 5, iOS 7.0.2.
谢谢.
解决方案:
按照 Greg 的指示,这解决了我的问题:将文件名设置为 @"image.png",并将 typeIdentifier 设置为 kUTTypePNG.
As Greg instructed, this solved my problem: set filename as @"image.png", and typeIdentifier to kUTTypePNG.
[messageController addAttachmentData:imgData typeIdentifier:(NSString *)kUTTypePNG filename:@"image.png"];
谢谢格雷格.
推荐答案
MFMessageComposeViewController 希望附件具有适合您上传的图像类型的正确扩展名.我通过使用 PNG 文件进行测试以及添加附件数据的以下变体进行了验证:
The MFMessageComposeViewController wants the attachment to have the correct extension for the type of image you're uploading. I verified by testing with a PNG file, and the following variations of adding the attachment data:
[messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image"];
[messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image.abc"];
[messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image.png"];
只有最后一个选项有效.我不需要更改 typeIdentifier,尽管选择与数据类型匹配的 UTI 可能更有意义.
Only the last option worked. I didn't need to change the typeIdentifier, although it probably would make sense to choose a UTI that matches the type of data.
UTI 的完整列表可在此处获得:系统声明的统一类型标识符(感谢@iWasRobbed!)
The full list of UTIs is available here: System-Declared Uniform Type Identifiers (Thanks @iWasRobbed!)
这篇关于MFMessageComposeViewController iOS7 addAttachmentData:typeIdentifier:filename: 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!