ios - 使用 sendResourceAtURL 给出 "Unsupported resource type"

标签 ios iphone xcode5 multipeer-connectivity

我正在尝试使用 sendResourceAtURL 发送我通过 imagePickerController 选择的图像。 我在方法 didFinishPickingMediaWithInfo 中得到的 URL 为

NSURL *refURL = [info objectForKey:UIImagePickerControllerReferenceURL];

但是当我打电话时

[self.mySession sendResourceAtURL:refURL withName:@"test" toPeer:peerid withCompletionHandler:^(NSError *error){
                if (error)....

我总是收到不支持的资源类型错误。 我相信网址的构造不正确,我需要其他东西。 当我使用 bundle 中的本地文件创建 url 时,它会正常传输。 有什么想法吗?

谢谢, 佐伊斯

最佳答案

我使用了多人群聊示例应用程序中提供的代码并且有效。您首先必须将文件写入文档目录,然后发送该 url。

// Don't block the UI when writing the image to documents
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    // We only handle a still image
    UIImage *imageToSave = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];

    // Save the new image to the documents directory
    NSData *pngData = UIImageJPEGRepresentation(imageToSave, 1.0);

    // Create a unique file name
    NSDateFormatter *inFormat = [NSDateFormatter new];
    [inFormat setDateFormat:@"yyMMdd-HHmmss"];
    NSString *imageName = [NSString stringWithFormat:@"image-%@.JPG", [inFormat stringFromDate:[NSDate date]]];
    // Create a file path to our documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageName];
    [pngData writeToFile:filePath atomically:YES]; // Write the file
    // Get a URL for this file resource
    NSURL *imageUrl = [NSURL fileURLWithPath:filePath];

    MCPeerID *peer = self.session.connectedPeers[0];
    [self.session sendResourceAtURL:imageUrl withName:imageName toPeer:peer withCompletionHandler:^(NSError *error) {
        if (error) {
            NSLog(@"Failed to send picture to %@, %@", peer.displayName, error.localizedDescription);
            return;
        }
        NSLog(@"Sent picture to %@", peer.displayName);
        //Clean up the temp file
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtURL:imageUrl error:nil];
    }];
});

关于ios - 使用 sendResourceAtURL 给出 "Unsupported resource type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19758980/

相关文章:

ios - 如何访问 xcode 项目中的 sqlite 文件?

ios - Pod安装Realm 3.1.1失败

iphone - 无法使用@interface将对象添加到NSMutableArray

xcode - 针对 Mac OS X 10.8 之前的目标进行编译时,Base.lproj/MainMenu.xib 不可用

iPhone - 将应用程序发送到后台

ios - 我可以复制一个 .db 文件到我的项目的根目录,然后插入或删除它的数据吗?

iOS 上的 Iphone5/ios6.1.3 和 Facebook SDK 3.2.1 - com.facebook.sdk 设备上出现错误 2

iphone - 在 UIWebView 中显示来自相机的 UIImage

git - .xccheckout 有树冲突

ios - Xcode - 找不到架构 x86_64 的符号(iOS Lib)