Whatsapp图片共享无法正常工作

时间:2014-12-09 13:56:09

标签: ios image whatsapp uidocumentinteraction

我的班级实现了UIDocumentInteractionControllerDelegate,我有以下属性:@property (nonatomic,retain) UIDocumentInteractionController * documentInteractionController;

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) {
    UIImage     *iconImage = [UIImage imageNamed:@"bg_iPhone_5.jpg"];
    NSString    *savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

    [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
    _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
    _documentInteractionController.UTI = @"net.whatsapp.image";
    _documentInteractionController.delegate = self;
    [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
}
else {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

您好我使用过这段代码但是图片共享无效。 DocumentInteractionController启动一个带有whatsapp图标的屏幕,当我点击它时,我的应用程序崩溃了。

3 个答案:

答案 0 :(得分:2)

以下是设置用于写出临时图像的URL的正确方法的代码:

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]) {
    NSError *error = nil;
    NSURL *documentURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];
    if (!documentURL) {
        NSLog(@"Error getting doucment directory: %@", error);
    }

    NSURL *tempFile = [documentURL URLByAppendingPathComponent:@"whatsAppTmp.wai"];
    UIImage *iconImage = [UIImage imageNamed:@"bg_iPhone_5.jpg"];

    NSData *imageData = UIImageJPEGRepresentation(iconImage, 1.0) ;
    if (![imageData writeToURL:tempFile options:NSDataWritingAtomic error:&error]) {
        NSLog(@"Error writing File: %@", error);
    }

    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:tempFile];
    self.documentInteractionController.UTI = @"net.whatsapp.image";
    self.documentInteractionController.delegate = self;
    [self.documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
}
else {
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

答案 1 :(得分:2)

要使用WhatsApp共享图像,请使用以下代码。它工作正常。

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]){

        UIImage     * iconImage = [UIImage imageNamed:@"my_account.png"];
        NSString    * savePath  = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];

        [UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];

        _documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
        _documentInteractionController.UTI = @"net.whatsapp.image";
        _documentInteractionController.delegate = self;

        [_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];


    } else {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

答案 2 :(得分:0)

对我来说,当我使用documentInteractionController作为属性时,问题得到解决。之前我在共享方法中使用它作为局部变量。