Archived Forum Post

Index of archived forum posts


How to display embedded image attachments?

Jul 20 '14 at 18:47

I am developing an email client using Chilkat Email Client Library. Many times users drag and drop images in the email body and send the email. Is there any API to retrieve/show such images?


Hi! You should use related items of image MIME type. Chilkat Email object contains related items and related items count. I use C# but I think that in your case code wil be the same.


Well I used the following to embed images in my WebView:

#import <AssetsLibrary/AssetsLibrary.h> /// REMEMBER TO ADD FRAMEWORK TO LINKER

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    // Obtain the path to save to
    NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

    // Get selected image filename
    __block NSString *imagePath = nil;
    NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset) {
        ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
        // Get path with filename at end
        imagePath = [documentsDirectory stringByAppendingPathComponent:[imageRep filename]];

        // Save the selected image inside the app
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        NSData *data = UIImagePNGRepresentation(image);

        // If image is saved, insert the image from correct path
        if ([data writeToFile:imagePath atomically:YES]) {
            // Embed the image into the webview (email)
            NSString *imageTag = [NSString stringWithFormat:@"\n<img src=\"%@\" width=\"100%%\"><br><br>", imagePath];
            NSString *oldHTML = [_emailBodyWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];
            NSString *newHTML = [NSString stringWithFormat:@"<html contenteditable=\"true\">%@%@</html>", oldHTML, imageTag];
            NSLog(@"\n\n NEW HTML: \n\n%@ \n\n >>> \n\n", newHTML);
            [_emailBodyWebView loadHTMLString:newHTML baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
            [_embededImages addObject:@{@"ImagePath": imagePath, @"ImageTitle": [imageRep filename]}];
        } else {
            NSLog(@"Couldn't save photo to Documents directory!");
            [picker dismissViewControllerAnimated:YES completion:nil];

        // Dismiss Photo Picker
        [picker dismissViewControllerAnimated:YES completion:^{ [_emailBodyWebView reload]; }];
    ALAssetsLibrary *assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];

In your "Sending Email Method":

NSLog(@"_embededImages: %@", _embededImages);

CkoEmail *email = [[CkoEmail alloc] init];  
for (int x = 0; x < images.count; x++) {
    NSString *imagePath = [[_embededImages objectAtIndex:x] valueForKey:@"ImagePath"];
    NSString *imageTitle = [[_embededImages objectAtIndex:x] valueForKey:@"ImageTitle"];
    [email AddRelatedFile2:imagePath filenameInHtml:imageTitle];

Few problems here. When embedding images, the title will be the imagePath name, if you twist them they wont work lol but you can play around with this as I just stopped using embedded images and will do everything as attachments only.

If you added the images, and delete them from WebView, they will still be in the "_embededImages" array for sending, so when you delete the images, it will still send.

I pasted this to help you, so feel free to continue and finish it off as I won't continue with embedded images. Hope this helps a little. :)