2016-07-26 3 views
0

私のプロジェクトでは、イメージを含める必要があるvCard(vcfファイル)を作成して送信する必要があります。私はvCardに画像を追加することができないという点を除いて、すべてを正しく行った。私は以下のコードを共有しました。Objective Cを使用してvCard(vcfファイル)に画像を添付する方法は?

- (IBAction)shareButtonPressed:(UIButton *)sender { 

    NSError *error; 
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]  stringByAppendingPathComponent:@"vCard.vcf"]; 
    [[self vCardRepresentation] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; 

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[@"Test", [NSURL fileURLWithPath:filePath]] applicationActivities:nil]; 
    activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypeMessage, UIActivityTypeCopyToPasteboard]; 
    [self presentViewController:activityVC animated:YES completion:^{ 

}]; 
} 

- (NSString *)vCardRepresentation 
{ 
    NSMutableArray *mutableArray = [[NSMutableArray alloc] init]; 
    NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"Rokon"], 1.0); 

    [mutableArray addObject:@"BEGIN:VCARD"]; 
    [mutableArray addObject:@"VERSION:3.0"]; 
    [mutableArray addObject:[NSString stringWithFormat:@"FN:%@", @"Rokon"]]; 
    [mutableArray addObject:[NSString stringWithFormat:@"TEL:%@",@"+8801811536248"]]; 
    [mutableArray addObject:[NSString stringWithFormat:@"PHOTO;BASE64:%@",[imageData base64EncodedDataWithOptions:0]]]; 
    [mutableArray addObject:@"END:VCARD"]; 

    return [mutableArray componentsJoinedByString:@"\n"]; 
} 

答えて

1
- (void)shareContact{ 
     [self emptySandbox]; 
     NSString *contactName = [NSString stringWithFormat:@"%@ %@",[Person sharedInstance].firstName, [Person sharedInstance].lastName]; 
     NSError *error; 
     NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.vcf", contactName]]; 
     [[self vCardRepresentation] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; 
     UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[contactName, [NSURL fileURLWithPath:filePath]] applicationActivities:nil]; 

     activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard]; 
     [activityVC setValue:contactName forKey:@"subject"]; 

     [self presentViewController:activityVC animated:YES completion:^{ 

     }]; 
} 


- (NSString *)vCardRepresentation 
    { 
NSMutableArray *mutableArray = [[NSMutableArray alloc] init]; 

NSData *imageData = UIImageJPEGRepresentation([Person sharedInstance].profileImage, 1.0); 

[mutableArray addObject:@"BEGIN:VCARD"]; 
[mutableArray addObject:@"VERSION:3.0"]; 

[mutableArray addObject:[NSString stringWithFormat:@"FN:%@", [NSString stringWithFormat:@"%@%@", [Person sharedInstance].firstName, [Person sharedInstance].lastName]]]; 
[mutableArray addObject:[NSString stringWithFormat:@"TEL:%@",[Person sharedInstance].phone]]; 
[mutableArray addObject:[NSString stringWithFormat:@"email:%@", [Person sharedInstance].email]]; 
[mutableArray addObject:[NSString stringWithFormat:@"PHOTO;BASE64;ENCODING=b;TYPE=JPEG:%@",[imageData base64EncodedStringWithOptions:0]]]; 
[mutableArray addObject:@"END:VCARD"]; 
return [mutableArray componentsJoinedByString:@"\n"]; 
} 

-(void)emptySandbox 
{ 
NSFileManager *fileMgr = [[NSFileManager alloc] init]; 
NSError *error = nil; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSArray *files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil]; 

while (files.count > 0) { 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSArray *directoryContents = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]; 
    if (error == nil) { 
     for (NSString *path in directoryContents) { 
      NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:path]; 
      BOOL removeSuccess = [fileMgr removeItemAtPath:fullPath error:&error]; 
      files = [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:nil]; 
      if (!removeSuccess) { 
       // Error 
      } 
     } 
    } else { 
     // Error 
    } 
} 
} 
関連する問題