【问题标题】:Attaching multiple image to an email将多个图像附加到电子邮件
【发布时间】:2012-08-21 01:47:02
【问题描述】:

我正在使用ELCimagepicker 将多个图像附加到电子邮件中,但我不确定如何使每个NSData 都独一无二,以便我可以将它们附加到我的电子邮件中。

>

- (void)launchController {
  ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] initWithNibName:@"ELCAlbumPickerController" bundle:[NSBundle mainBundle]];
  ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
  [albumController setParent:elcPicker];
  [elcPicker setDelegate:self];
  [self presentModalViewController:elcPicker animated:YES];
 }

- (void)elcImagePickerController:(ELCImagePickerController *)picker     didFinishPickingMediaWithInfo:(NSArray *)info {

  [self dismissModalViewControllerAnimated:YES];
  int i = 0;
  for(NSDictionary *dict in info) {
     i++;
     UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict     objectForKey:UIImagePickerControllerOriginalImage]];
     NSData *name = UIImageJPEGRepresentation(imageview, 1.0);
   }

}

【问题讨论】:

标签: iphone ios image email attachment


【解决方案1】:

从此链接获取应用 https://github.com/elc/ELCImagePickerController

以及以下代码的更改

使用for循环

-(IBAction)launchController 

{

ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] initWithNibName:@"ELCAlbumPickerController" bundle:[NSBundle mainBundle]];    
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
[albumController setParent:elcPicker];
[elcPicker setDelegate:self];

ELCImagePickerDemoAppDelegate *app = (ELCImagePickerDemoAppDelegate *)[[UIApplication sharedApplication] delegate];
[app.viewController presentModalViewController:elcPicker animated:YES];
[elcPicker release];
[albumController release];

[self buttonPressed];

}



- (void)buttonPressed
{
    // Create image picker controller
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // Set source to the camera
    imagePicker.sourceType =  UIImagePickerControllerSourceTypeSavedPhotosAlbum;

    // Delegate is self
    imagePicker.delegate = self;

    // Allow editing of image ?
    imagePicker.allowsImageEditing = NO;

    // Show image picker
    [self presentModalViewController:imagePicker animated:YES]; 
}







- (void)emailImage:(UIImage *)image
{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    // Set the subject of email
    [picker setSubject:@"Picture from my iPhone!"];

    // Add email addresses
    // Notice three sections: "to" "cc" and "bcc"   
    [picker setToRecipients:[NSArray arrayWithObjects:@"emailaddress1@domainName.com", @"emailaddress2@domainName.com", nil]];
    [picker setCcRecipients:[NSArray arrayWithObject:@"emailaddress3@domainName.com"]]; 
    [picker setBccRecipients:[NSArray arrayWithObject:@"emailaddress4@domainName.com"]];

    // Fill out the email body text
    NSString *emailBody = @"I just took this picture, check it out.";


    for (int i=0; i<[imagedelegate.imgarray count]; i++) 
    {

        UIImageView *image=[[UIImageView alloc] init];
        image=[imagedelegate.imgarray objectAtIndex:i];

    // This is not an HTML formatted email
    [picker setMessageBody:emailBody isHTML:NO];

    // Create NSData object as PNG image data from camera image
    NSData *data = UIImagePNGRepresentation(image);

    // Attach image data to the email
    // 'CameraImage.png' is the file name that will be attached to the email
    [picker addAttachmentData:data mimeType:@"image/png" fileName:@"CameraImage"];

    // Show email view  
    [self presentModalViewController:picker animated:YES];

    // Release picker
    [picker release];
    }
}



- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    // Dismiss the camera
    [self dismissModalViewControllerAnimated:YES];

    // Pass the image from camera to method that will email the same
    // A delay is needed so camera view can be dismissed
    [self performSelector:@selector(emailImage:) withObject:image afterDelay:1.0];

    // Release picker
    [picker release];
}

【讨论】:

    【解决方案2】:

    从这个链接http://code.google.com/p/objective-zip/downloads/list (Objective-zip) 下载并将 Objective-Zip、MiniZip 和 ZLib 拖放到您的项目中。导入文件:ZipFile.h、ZipException.h、FileInZipInfo.h、ZipWriteStream。 h、ZipReadStream.h、zlib.h

    使用以下代码:

    NSString *stringPath1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]];
        NSString *FileName=[stringPath1 stringByAppendingPathComponent:@"Your file name"];
    
    
        NSString *stringPath=[stringPath1 stringByAppendingPathComponent:[@"Your file name" stringByAppendingFormat:@".zip"]];
        NSArray *files = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:FileName error:&error];
        ZipFile *zipFile = [[ZipFile alloc]initWithFileName:stringPath mode:ZipFileModeCreate];
    
        for(int i = 0;i<files.count;i++){
    
            id myArrayElement = [files  objectAtIndex:i];
            NSLog(@"add %@", myArrayElement);
    
            NSString *path = [FileName stringByAppendingPathComponent:myArrayElement];
            NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:path error:&error];
            NSDate *Date = [attributes objectForKey:NSFileCreationDate];
    
            ZipWriteStream *streem = [zipFile writeFileInZipWithName:myArrayElement fileDate:Date compressionLevel:ZipCompressionLevelBest];
            NSData *data = [NSData dataWithContentsOfFile:path];
            [streem writeData:data];
            [streem finishedWriting];
        }
    
        [zipFile close];
    

    【讨论】:

    • 谢谢,只是想知道我把图像数据放在哪个部分?
    • 您可以将所有图像放在一个数组中并使用此循环: for(int i = 0;i
    • 嗯,我添加了文件,但似乎无法#import
    • 您必须从这些导入的文件中删除 ARC,因为您的项目必须在 ARC 上运行
    猜你喜欢
    • 1970-01-01
    • 2010-10-06
    • 2016-12-29
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    相关资源
    最近更新 更多