将多个图像附加到电子邮件

时间:2012-08-21 01:47:02

标签: iphone ios image email attachment

我正在使用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);
   }

}

2 个答案:

答案 0 :(得分:2)

从此链接获取应用 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];
}

答案 1 :(得分:0)

从这个链接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];