0
在我的項目中,我需要創建併發送一個必須包含圖像的vCard(vcf文件)。除了我無法將圖像添加到電子名片之外,我所做的一切都是正確的。我已經在下面分享了我的代碼。如何使用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"];
}