diff options
author | Jonathan Willing <jwilling@me.com> | 2013-05-12 17:11:54 -0500 |
---|---|---|
committer | Jonathan Willing <jwilling@me.com> | 2013-05-12 17:11:54 -0500 |
commit | 2509aa61d11a8ddca19b3fa5b1c560c111afed76 (patch) | |
tree | dee39c51dda04b49e4f87eec5a0a1da457748b69 /example | |
parent | ce800f23933baa4fa0e86e3277aeb4a45e558532 (diff) |
implement the ability to load cid and x-mailcore-images in the iOS demo
Diffstat (limited to 'example')
-rw-r--r-- | example/ios/iOS UI Test/iOS UI Test.xcodeproj/project.pbxproj | 20 | ||||
-rwxr-xr-x | example/ios/iOS UI Test/iOS UI Test/MCOCIDURLProtocol.mm | 2 | ||||
-rwxr-xr-x | example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm | 70 | ||||
-rw-r--r-- | example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js | 29 | ||||
-rwxr-xr-x | example/ios/iOS UI Test/iOS UI Test/MCTMsgViewController.mm (renamed from example/ios/iOS UI Test/iOS UI Test/MCTMsgViewController.m) | 95 | ||||
-rw-r--r-- | example/ios/iOS UI Test/iOS UI Test/MasterViewController.m | 3 |
6 files changed, 163 insertions, 56 deletions
diff --git a/example/ios/iOS UI Test/iOS UI Test.xcodeproj/project.pbxproj b/example/ios/iOS UI Test/iOS UI Test.xcodeproj/project.pbxproj index bf9f8cbb..e49346ed 100644 --- a/example/ios/iOS UI Test/iOS UI Test.xcodeproj/project.pbxproj +++ b/example/ios/iOS UI Test/iOS UI Test.xcodeproj/project.pbxproj @@ -24,9 +24,12 @@ AB9EAE3617036FD700D750C7 /* SettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = AB9EAE3417036FD600D750C7 /* SettingsViewController.m */; }; AB9EAE3717036FD700D750C7 /* SettingsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = AB9EAE3517036FD700D750C7 /* SettingsViewController.xib */; }; AB9EAE40170374D900D750C7 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB9EAE3F170374D900D750C7 /* Security.framework */; }; + ABE40268173F0151007F1FB3 /* MCOMessageViewScript.js in Sources */ = {isa = PBXBuildFile; fileRef = ABE40267173F0151007F1FB3 /* MCOMessageViewScript.js */; }; + ABE40269173F0282007F1FB3 /* MCOMessageViewScript.js in Resources */ = {isa = PBXBuildFile; fileRef = ABE40267173F0151007F1FB3 /* MCOMessageViewScript.js */; }; + ABE4026B173F3FCE007F1FB3 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ABE4026A173F3FCE007F1FB3 /* ImageIO.framework */; }; B12AAA3317322654003551C7 /* MCOCIDURLProtocol.mm in Sources */ = {isa = PBXBuildFile; fileRef = B12AAA2E17322654003551C7 /* MCOCIDURLProtocol.mm */; }; B12AAA3417322654003551C7 /* MCOMessageView.mm in Sources */ = {isa = PBXBuildFile; fileRef = B12AAA3017322654003551C7 /* MCOMessageView.mm */; }; - B12AAA3517322654003551C7 /* MCTMsgViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B12AAA3217322654003551C7 /* MCTMsgViewController.m */; }; + B12AAA3517322654003551C7 /* MCTMsgViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = B12AAA3217322654003551C7 /* MCTMsgViewController.mm */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -100,12 +103,14 @@ AB9EAE3417036FD600D750C7 /* SettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SettingsViewController.m; sourceTree = "<group>"; }; AB9EAE3517036FD700D750C7 /* SettingsViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SettingsViewController.xib; sourceTree = "<group>"; }; AB9EAE3F170374D900D750C7 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; }; + ABE40267173F0151007F1FB3 /* MCOMessageViewScript.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = MCOMessageViewScript.js; sourceTree = "<group>"; }; + ABE4026A173F3FCE007F1FB3 /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; }; B12AAA2D17322654003551C7 /* MCOCIDURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCOCIDURLProtocol.h; sourceTree = "<group>"; }; B12AAA2E17322654003551C7 /* MCOCIDURLProtocol.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MCOCIDURLProtocol.mm; sourceTree = "<group>"; }; B12AAA2F17322654003551C7 /* MCOMessageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCOMessageView.h; sourceTree = "<group>"; }; B12AAA3017322654003551C7 /* MCOMessageView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MCOMessageView.mm; sourceTree = "<group>"; }; B12AAA3117322654003551C7 /* MCTMsgViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCTMsgViewController.h; sourceTree = "<group>"; }; - B12AAA3217322654003551C7 /* MCTMsgViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MCTMsgViewController.m; sourceTree = "<group>"; }; + B12AAA3217322654003551C7 /* MCTMsgViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MCTMsgViewController.mm; sourceTree = "<group>"; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -114,6 +119,7 @@ buildActionMask = 2147483647; files = ( AB7BA446171389CC00104953 /* libMailCore-ios.a in Frameworks */, + ABE4026B173F3FCE007F1FB3 /* ImageIO.framework in Frameworks */, AB9EAE40170374D900D750C7 /* Security.framework in Frameworks */, AB9EAE06170368F000D750C7 /* UIKit.framework in Frameworks */, AB9EAE08170368F000D750C7 /* Foundation.framework in Frameworks */, @@ -168,6 +174,7 @@ AB9EAE04170368F000D750C7 /* Frameworks */ = { isa = PBXGroup; children = ( + ABE4026A173F3FCE007F1FB3 /* ImageIO.framework */, AB7BA4471713913F00104953 /* CFNetwork.framework */, AB9EAE3F170374D900D750C7 /* Security.framework */, AB9EAE05170368F000D750C7 /* UIKit.framework */, @@ -191,12 +198,13 @@ B12AAA2E17322654003551C7 /* MCOCIDURLProtocol.mm */, B12AAA2F17322654003551C7 /* MCOMessageView.h */, B12AAA3017322654003551C7 /* MCOMessageView.mm */, + ABE40267173F0151007F1FB3 /* MCOMessageViewScript.js */, B12AAA3117322654003551C7 /* MCTMsgViewController.h */, - B12AAA3217322654003551C7 /* MCTMsgViewController.m */, - AB9EAE0C170368F000D750C7 /* Supporting Files */, + B12AAA3217322654003551C7 /* MCTMsgViewController.mm */, AB9EAE3317036FD600D750C7 /* SettingsViewController.h */, AB9EAE3417036FD600D750C7 /* SettingsViewController.m */, AB9EAE3517036FD700D750C7 /* SettingsViewController.xib */, + AB9EAE0C170368F000D750C7 /* Supporting Files */, AB665BC917134336007F2151 /* common */, ); path = "iOS UI Test"; @@ -311,6 +319,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + ABE40269173F0282007F1FB3 /* MCOMessageViewScript.js in Resources */, AB9EAE10170368F000D750C7 /* InfoPlist.strings in Resources */, AB9EAE18170368F000D750C7 /* Default.png in Resources */, AB9EAE1A170368F000D750C7 /* Default@2x.png in Resources */, @@ -334,7 +343,8 @@ AB665BCD17134336007F2151 /* FXKeychain.m in Sources */, B12AAA3317322654003551C7 /* MCOCIDURLProtocol.mm in Sources */, B12AAA3417322654003551C7 /* MCOMessageView.mm in Sources */, - B12AAA3517322654003551C7 /* MCTMsgViewController.m in Sources */, + B12AAA3517322654003551C7 /* MCTMsgViewController.mm in Sources */, + ABE40268173F0151007F1FB3 /* MCOMessageViewScript.js in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/example/ios/iOS UI Test/iOS UI Test/MCOCIDURLProtocol.mm b/example/ios/iOS UI Test/iOS UI Test/MCOCIDURLProtocol.mm index 6a6d0a03..f07dbc0f 100755 --- a/example/ios/iOS UI Test/iOS UI Test/MCOCIDURLProtocol.mm +++ b/example/ios/iOS UI Test/iOS UI Test/MCOCIDURLProtocol.mm @@ -56,6 +56,7 @@ { self = [super initWithRequest:request cachedResponse:cachedResponse client:client]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_downloaded:) name:MCOCIDURLProtocolDownloadedNotification object:nil]; + //NSLog(@"%@", [request mainDocumentURL]); return self; } @@ -82,6 +83,7 @@ - (void) startLoading { //NSLog(@"waiting for %p %@", self, [self _partUniqueID]); + //NSLog(@"%@", [self request]); if ([self _data] != NULL) { [[self class] partDownloadedMessage:[self _message] partUniqueID:[self _partUniqueID] data:[self _data]]; } diff --git a/example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm b/example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm index 87e07b57..0ba10ade 100755 --- a/example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm +++ b/example/ios/iOS UI Test/iOS UI Test/MCOMessageView.mm @@ -90,7 +90,68 @@ MCAssert(0); } } - [_webView loadHTMLString:content baseURL:nil]; + NSLog(@"%s",__PRETTY_FUNCTION__); + + if (content == nil) { + [_webView loadHTMLString:@"" baseURL:/*[NSURL URLWithString:urlString]*/nil]; + return; + } + + NSMutableString * html = [NSMutableString string]; + NSURL * jsURL = [[NSBundle mainBundle] URLForResource:@"MCOMessageViewScript" withExtension:@"js"]; + [html appendFormat:@"<html><head><script src=\"%@\"></script></head><body'>%@</body><iframe src='x-mailcore-msgviewloaded:' style='width: 0px; height: 0px; border: none;'></iframe></html>", + [jsURL absoluteString], content]; + [_webView loadHTMLString:html baseURL:nil]; +} + +- (void) _loadImages +{ + NSLog(@"%s",__PRETTY_FUNCTION__); + NSString * result = [_webView stringByEvaluatingJavaScriptFromString:@"findCIDImageURL()"]; + NSData * data = [result dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error = nil; + NSArray * imagesURLStrings = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; + + for(NSString * urlString in imagesURLStrings) { + MCOAbstractPart * part = nil; + NSURL * url; + + url = [NSURL URLWithString:urlString]; + if ([MCOCIDURLProtocol isCID:url]) { + part = [self _partForCIDURL:url]; + } + else if ([MCOCIDURLProtocol isXMailcoreImage:url]) { + NSString * specifier = [url resourceSpecifier]; + NSString * partUniqueID = specifier; + part = [self _partForUniqueID:partUniqueID]; + } + + NSString * partUniqueID = [part uniqueID]; + NSData * data = [[self delegate] MCOMessageView:self dataForPartWithUniqueID:partUniqueID]; + if (data == NULL) { + [[self delegate] MCOMessageView:self fetchDataForPartWithUniqueID:partUniqueID downloadedFinished:^(NSError * error) { + NSData * downloadedData = [[self delegate] MCOMessageView:self dataForPartWithUniqueID:partUniqueID]; + NSData * previewData = [[self delegate] MCOMessageView:self previewForData:downloadedData isHTMLInlineImage:[MCOCIDURLProtocol isCID:url]]; + NSString *filename = [NSString stringWithFormat:@"%lu", (unsigned long)urlString.hash]; + NSURL *cacheURL = [self cacheJPEGImageData:previewData withFilename:filename]; + + NSString *replaceScript = [NSString stringWithFormat:@"replaceImageSrc(\"%@\", \"%@\")", urlString, cacheURL.absoluteString]; + [_webView stringByEvaluatingJavaScriptFromString:replaceScript]; + + [MCOCIDURLProtocol partDownloadedMessage:_message partUniqueID:partUniqueID data:previewData]; + }]; + } + [MCOCIDURLProtocol startLoadingWithMessage:_message + partUniqueID:partUniqueID + data:data + request:nil]; + } +} + +- (NSURL *)cacheJPEGImageData:(NSData *)imageData withFilename:(NSString *)filename { + NSString *path = [[NSTemporaryDirectory() stringByAppendingPathComponent:filename] stringByAppendingPathExtension:@"jpg"]; + [imageData writeToFile:path atomically:YES]; + return [NSURL fileURLWithPath:path]; } - (MCOAbstractPart *) _partForCIDURL:(NSURL *)url @@ -121,7 +182,7 @@ NSURLRequest *responseRequest = [self webView:webView resource:nil willSendRequest:request redirectResponse:nil fromDataSource:nil]; //NSLog(@"responseRequest:%@", responseRequest); - + if(responseRequest == request) { return YES; } else { @@ -135,6 +196,11 @@ { MCOAbstractPart * part = NULL; + if ([[[request URL] scheme] isEqualToString:@"x-mailcore-msgviewloaded"]) { + [self _loadImages]; + return request; + } + if ([MCOCIDURLProtocol isCID:[request URL]]) { part = [self _partForCIDURL:[request URL]]; } diff --git a/example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js b/example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js new file mode 100644 index 00000000..67510c74 --- /dev/null +++ b/example/ios/iOS UI Test/iOS UI Test/MCOMessageViewScript.js @@ -0,0 +1,29 @@ + +var imageElements = function() { + var imageNodes = document.getElementsByTagName('img'); + return [].slice.call(imageNodes); +} + +var findCIDImageURL = function() { + var images = imageElements(); + + var imgLinks = []; + for (var i = 0; i < images.length; i++) { + var url = images[i].getAttribute('src'); + if (url.indexOf('cid:') == 0 || url.indexOf('x-mailcore-image:') == 0) + imgLinks.push(url); + } + return JSON.stringify(imgLinks); +} + +var replaceImageSrc = function(original, local) { + var images = imageElements(); + + for (var i = 0; i < images.length; i++) { + var url = images[i].getAttribute('src'); + if (url.indexOf(original) == 0) { + images[i].setAttribute('src', local); + break; + } + } +} diff --git a/example/ios/iOS UI Test/iOS UI Test/MCTMsgViewController.m b/example/ios/iOS UI Test/iOS UI Test/MCTMsgViewController.mm index 6e3e8d52..43499d9e 100755 --- a/example/ios/iOS UI Test/iOS UI Test/MCTMsgViewController.m +++ b/example/ios/iOS UI Test/iOS UI Test/MCTMsgViewController.mm @@ -7,7 +7,8 @@ // #import "MCTMsgViewController.h" - +#import <CoreGraphics/CoreGraphics.h> +#import <ImageIO/ImageIO.h> #import "MCOMessageView.h" @interface MCTMsgViewController () <MCOMessageViewDelegate> @@ -68,7 +69,7 @@ - (void) setMessage:(MCOIMAPMessage *)message { - NSLog(@"set message : %@", message); + if (mailcore::logEnabled) NSLog(@"set message : %@", message); for(MCOOperation * op in _ops) { [op cancel]; } @@ -87,7 +88,7 @@ - (MCOIMAPFetchContentOperation *) _fetchIMAPPartWithUniqueID:(NSString *)partUniqueID folder:(NSString *)folder { - NSLog(@"%@ is missing, fetching", partUniqueID); + if (mailcore::logEnabled) NSLog(@"%@ is missing, fetching", partUniqueID); if ([_pending containsObject:partUniqueID]) { return NULL; @@ -110,7 +111,7 @@ [_ops removeObject:op]; [_storage setObject:data forKey:partUniqueID]; [_pending removeObject:partUniqueID]; - NSLog(@"downloaded %@", partUniqueID); + if (mailcore::logEnabled) NSLog(@"downloaded %@", partUniqueID); [self _callbackForPartUniqueID:partUniqueID error:nil]; }]; @@ -124,11 +125,11 @@ typedef void (^DownloadCallback)(NSError * error); { NSArray * blocks; blocks = [_callbacks objectForKey:partUniqueID]; - NSLog(@"%@", blocks); + if (mailcore::logEnabled) NSLog(@"%@", blocks); for(DownloadCallback block in blocks) { block(error); } - NSLog(@"done: %@", blocks); + if (mailcore::logEnabled) NSLog(@"done: %@", blocks); } - (NSString *) MCOMessageView_templateForAttachment:(MCOMessageView *)view @@ -198,7 +199,7 @@ typedef void (^DownloadCallback)(NSError * error); { MCOIMAPFetchContentOperation * op = [self _fetchIMAPPartWithUniqueID:partUniqueID folder:_folder]; [op setProgress:^(unsigned int current, unsigned int maximum) { - NSLog(@"progress content: %u/%u", current, maximum); + if (mailcore::logEnabled) NSLog(@"progress content: %u/%u", current, maximum); }]; [_ops addObject:op]; if (downloadFinished != NULL) { @@ -225,47 +226,45 @@ typedef void (^DownloadCallback)(NSError * error); #define IMAGE_PREVIEW_HEIGHT 300 #define IMAGE_PREVIEW_WIDTH 500 -- (NSData *) _convertToJPEGData:(NSData *)data -{ -// CGImageRef imageSource; -// CGImageRef thumbnail; -// NSMutableDictionary * info; -// int width; -// int height; -// float quality; -// -// width = IMAGE_PREVIEW_WIDTH; -// height = IMAGE_PREVIEW_HEIGHT; -// quality = 1.0; -// -// imageSource = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL); -// if (imageSource == NULL) -// return nil; -// -// info = [[NSMutableDictionary alloc] init]; -// [info setObject:(id) kCFBooleanTrue forKey:(id) kCGImageSourceCreateThumbnailWithTransform]; -// [info setObject:(id) kCFBooleanTrue forKey:(id) kCGImageSourceCreateThumbnailFromImageAlways]; -// [info setObject:(id) [NSNumber numberWithFloat:(float) IMAGE_PREVIEW_WIDTH] forKey:(id) kCGImageSourceThumbnailMaxPixelSize]; -// thumbnail = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (CFDictionaryRef) info); -// [info release]; -// -// CGImageDestinationRef destination; -// NSMutableData * destData = [NSMutableData data]; -// -// destination = CGImageDestinationCreateWithData((CFMutableDataRef) destData, -// (CFStringRef) @"public.jpeg", -// 1, NULL); -// -// CGImageDestinationAddImage(destination, thumbnail, NULL); -// CGImageDestinationFinalize(destination); -// -// CFRelease(destination); -// -// CFRelease(thumbnail); -// CFRelease(imageSource); -// -// return destData; - return nil; +- (NSData *) _convertToJPEGData:(NSData *)data { + CGImageSourceRef imageSource; + CGImageRef thumbnail; + NSMutableDictionary * info; + int width; + int height; + float quality; + + width = IMAGE_PREVIEW_WIDTH; + height = IMAGE_PREVIEW_HEIGHT; + quality = 1.0; + + imageSource = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL); + if (imageSource == NULL) + return nil; + + info = [[NSMutableDictionary alloc] init]; + [info setObject:(id) kCFBooleanTrue forKey:(id) kCGImageSourceCreateThumbnailWithTransform]; + [info setObject:(id) kCFBooleanTrue forKey:(id) kCGImageSourceCreateThumbnailFromImageAlways]; + [info setObject:(id) [NSNumber numberWithFloat:(float) IMAGE_PREVIEW_WIDTH] forKey:(id) kCGImageSourceThumbnailMaxPixelSize]; + thumbnail = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (__bridge CFDictionaryRef) info); + //[info release]; + + CGImageDestinationRef destination; + NSMutableData * destData = [NSMutableData data]; + + destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef) destData, + + (CFStringRef) @"public.jpeg", + + 1, NULL); + + CGImageDestinationAddImage(destination, thumbnail, NULL); + CGImageDestinationFinalize(destination); + + CFRelease(destination); + + CFRelease(thumbnail); + CFRelease(imageSource); + + return destData; } @end diff --git a/example/ios/iOS UI Test/iOS UI Test/MasterViewController.m b/example/ios/iOS UI Test/iOS UI Test/MasterViewController.m index 493c8e5b..bf72733e 100644 --- a/example/ios/iOS UI Test/iOS UI Test/MasterViewController.m +++ b/example/ios/iOS UI Test/iOS UI Test/MasterViewController.m @@ -80,7 +80,7 @@ NSLog(@"fetched all messages."); NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"header.date" ascending:NO]; - strongSelf.messages = [messages sortedArrayUsingDescriptors:@[sort]];//[NSArray arrayWithArray:messages]; + strongSelf.messages = [messages sortedArrayUsingDescriptors:@[sort]]; [strongSelf.tableView reloadData]; }]; } @@ -135,6 +135,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { MCOIMAPMessage *msg = self.messages[indexPath.row]; MCTMsgViewController *vc = [[MCTMsgViewController alloc] init]; + vc.folder = @"INBOX"; vc.message = msg; vc.session = self.imapSession; [self.navigationController pushViewController:vc animated:YES]; |