title: MFMailComposeViewController: Attaching Images from Photo Gallery
body: <p>I am having a bit of trouble attaching images from the Photo Gallery to an email.  </p>
  
  <p>Basically, one of the features of my application allow the user to take photos.  When they snap the shot, I record the URL Reference to the image in Core Data.  I understand that you have to go through the <code>ALAssetRepresentation</code> to get to the image.  I have this up and running within my application for when the user wants to review an image that they have taken.</p>
  
  <p>I am now attempting to allow the user to attach all of the photos taken for an event to an email.  While doing this, I iterate through the Core Data entity that stores the URL References, call a method that returns a <code>UIImage</code> from the <code>ALAssetsLibrary</code> and then attaches it using the <code>NSData</code>/<code>UIImageJPEGRepresentation</code> and <code>MFMailComposeViewController</code>/<code>addAttachmentData</code> methods.</p>
  
  <p>Problem is: When the email is presented to the user, there are small blue squares representing the images and the image is not attached.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>- (void)sendReportReport
  {
  
      if ([MFMailComposeViewController canSendMail])
      {
  
          MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
  
          mailer.mailComposeDelegate = self;
  
          [mailer setSubject:@"Log: Report"];
  
          NSArray *toRecipients = [NSArray arrayWithObjects:@"someone@someco.com", nil];
          [mailer setToRecipients:toRecipients];
  
  
          NSError *error;
  
          NSFetchRequest *fetchPhotos = [[NSFetchRequest alloc] init];
          NSEntityDescription *entity = [NSEntityDescription 
                                         entityForName:@"Photo" inManagedObjectContext:__managedObjectContext];
          [fetchPhotos setEntity:entity];
          NSArray *fetchedPhotos = [__managedObjectContext executeFetchRequest:fetchPhotos error:&amp;error];
          int counter;
  
          for (NSManagedObject *managedObject in fetchedPhotos ) {
              Photo *photo = (Photo *)managedObject;
  
  //            UIImage *myImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", counter++]];
              NSData *imageData = UIImageJPEGRepresentation([self getImage:photo.referenceURL], 0.5);
  //            NSData *imageData = UIImagePNGRepresentation([self getImage:photo.referenceURL]);
  //            [mailer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:[NSString stringWithFormat:@"%i", counter]];  
              [mailer addAttachmentData:imageData mimeType:@"image/jpeg" fileName:[NSString stringWithFormat:@"a.jpg"]];  
  
              counter++;
  
          }
  
  
          NSString *emailBody = [self getEmailBody];
  
          [mailer setMessageBody:emailBody isHTML:NO];
  
          [self presentModalViewController:mailer animated:YES];
  
      }
      else
      {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                          message:@"Your device doesn't support the composer sheet"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
          [alert show];
  
      }
  
  }
  </code></pre>
  
  <p>and the method that returns the <code>UIImage</code>:</p>
  
  <pre><code>#pragma mark - Get Photo from Asset Library
  + (ALAssetsLibrary *)defaultAssetsLibrary {
      static dispatch_once_t pred = 0;
      static ALAssetsLibrary *library = nil;
      dispatch_once(&amp;pred, ^{
          library = [[ALAssetsLibrary alloc] init];
      });
      return library; 
  }
  
  - (UIImage *)getImage:(NSString *)URLReference
  {
  
      __block UIImage *xPhoto = nil;
  
      ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
      {
          UIImage *xImage;
  
          // get the image
          ALAssetRepresentation *rep = [myasset defaultRepresentation];
          CGImageRef iref = [rep fullScreenImage];
  
          if (iref) {
              xImage = [UIImage imageWithCGImage:iref];
          }
  
          xPhoto = xImage;
  
      };
  
  
      ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
      {
          NSLog(@"Error fetching photo: %@",[myerror localizedDescription]);
      };
  
  
      NSURL *asseturl = [NSURL URLWithString:URLReference];
  
      // create library and set callbacks
      ALAssetsLibrary *al = [DetailsViewController defaultAssetsLibrary];
      [al assetForURL:asseturl 
          resultBlock:resultblock
         failureBlock:failureblock];   
  
      return xPhoto;
  
  }
  </code></pre>
  
  <p>NOTE: This above code does run, it simply does not attach the image.  Also, note, I am able to successfully attach images from the gallery within my application, as long at I have set them into a <code>UIImageView</code>.Image already (basically, I am taking the pointer to the image from the <code>UIImageView</code> and passing it to the <code>addAttachmentData</code> method.)  It is just when I attempt to iterate through Core Data and attach without first setting the image into a <code>UIImageView</code> that I have the trouble.</p>
  
  <p>Any tips would be greatly appreciated!</p>
  
  <p>Thanks!
  Jason</p>