title: Getting a UIImage from an ALASSET to use ad an attachment when sending an email
body: <p>I have a collection view which contains ALASSETS, I want to be able to select an item (photo) in the collection view and send it as an attachment in an email.
  The collection view is based on a tutorial, iPhone Programming Tutorial: Creating An Image Gallery this is a recent post so it should be up to date.</p>
  
  <p>The code executed when the user selects a photo is as follows.</p>
  
  <pre><code>-(void) collectionView:(UICollectionView *)collectionView         didSelectItemAtIndexPath:`enter code here`(NSIndexPath *)indexPath
  { 
   ALAsset *asset = self.assets[indexPath.row];
   ALAssetRepresentation *defaultRep = [asset defaultRepresentation];
   UIImage *image = [UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0];
  
  
  
  [self.testImage setImage:[UIImage imageWithCGImage:[defaultRep fullScreenImage] scale:[defaultRep scale] orientation:0]];
  [self.testImage2 setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
  
  // send email
  if ([MFMailComposeViewController canSendMail]) {
  
      MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
      controller.mailComposeDelegate = self;
      [controller setToRecipients:[NSArray arrayWithObject:@"rasmus4200@gmail.com"]];
      [controller setSubject:@"Subject Goes Here."];
      [controller setMessageBody:@"Your message goes here." isHTML:NO];
  
      // Attach an image to the email
      //NSData *myData = UIImagePNGRepresentation(coolImage);
      //NSData *myData = UIImageJPEGRepresentation([UIImage imageWithCGImage:[asset thumbnail]], 1.0);
      NSData *myData = UIImageJPEGRepresentation(image, 1.0);
      [controller addAttachmentData:myData mimeType:@"image/jpg" fileName:@"coolImage.jpg"];
  
      [self presentModalViewController:controller animated:YES];
  }
  else {
      NSLog(@"Device is unable to send email in its current state.");
  }
  </code></pre>
  
  <p>The important lines of code would seem to be .</p>
  
  <p>I should now have an image which I can pass to the email as an attachment as follows</p>
  
  <pre><code>NSData *myData = UIImageJPEGRepresentation(image, 1.0);
  </code></pre>
  
  <p>But when I view my email there is no image.</p>
  
  <p>I thought maybe I was not actually getting the asset so i did the following test
  I decided to include the asset thumbnail image as follows which is now commented</p>
  
  <pre><code>NSData *myData = UIImageJPEGRepresentation([UIImage imageWithCGImage:[asset thumbnail]], 1.0);
  </code></pre>
  
  <p>and this works fine it created the email with the thumbnail</p>
  
  <p>This problem has me tearing my hair out so any help will be gratefully accepted.</p>
  
  <p>I have tried the following in many shapes and forms to retrieve the image but nothing seems to work </p>
  
  <p>Attempt 1 :</p>
  
  <pre><code>// Set up Blocks
  ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset)
  {
      UIImage *coolImage;
  
      // get the image
      ALAssetRepresentation *rep = [asset defaultRepresentation];
      CGImageRef iref = [rep fullResolutionImage];
      if (iref) {
          coolImage = [UIImage imageWithCGImage:iref];
      }
  
      // send email
      if ([MFMailComposeViewController canSendMail]) {
  
          MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
          controller.mailComposeDelegate = self;
          [controller setToRecipients:[NSArray arrayWithObject:@"rasmus4200@gmail.com"]];
          [controller setSubject:@"Subject Goes Here."];
          [controller setMessageBody:@"Your message goes here." isHTML:NO];
  
          // Attach an image to the email
          //NSData *myData = UIImagePNGRepresentation(coolImage);
          NSData *myData = UIImageJPEGRepresentation(coolImage, 1.0);
          [controller addAttachmentData:myData mimeType:@"image/jpg" fileName:@"coolImage.jpg"];
  
          [self presentModalViewController:controller animated:YES];
      }
      else {
          NSLog(@"Device is unable to send email in its current state.");
      }
  };
  
  
  ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
  {
      NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
  };
  
  
  
  // GET THE URL of image
  NSURL *url = [[NSURL alloc] init];
  url = asset.defaultRepresentation.url;
  
  // create library and set callbacks
  ALAssetsLibrary *al = [ConDetailsViewController defaultAssetsLibrary];
  [al assetForURL:url
      resultBlock:resultblock
     failureBlock:failureblock];
  
  }
  </code></pre>