Skip to content

Instantly share code, notes, and snippets.

@jnjosh
Created May 18, 2014 19:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnjosh/6f078e93f1df9a3dc14c to your computer and use it in GitHub Desktop.
Save jnjosh/6f078e93f1df9a3dc14c to your computer and use it in GitHub Desktop.
TWTHasselhoffImageProtocol
//
// TWTHasselhoffImageProtocol.h
// TestURLProtocol
//
// Created by Josh Johnson on 5/9/14.
// Copyright (c) 2014 Two Toasters, LLC. All rights reserved.
//
@import Foundation;
@interface TWTHasselhoffImageProtocol : NSURLProtocol
@end
//
// TWTHasselhoffImageProtocol.m
// TestURLProtocol
//
// Created by Josh Johnson on 5/9/14.
// Copyright (c) 2014 Two Toasters, LLC. All rights reserved.
//
#import "TWTHasselhoffImageProtocol.h"
@implementation TWTHasselhoffImageProtocol
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
NSSet *validContentTypes = [NSSet setWithArray:@[ @"image/png",
@"image/jpg",
@"image/jpeg" ]];
return [validContentTypes containsObject:request.allHTTPHeaderFields[@"Content-Type"]];
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
return request;
}
- (NSCachedURLResponse *)cachedResponse
{
return nil;
}
- (void)startLoading
{
id <NSURLProtocolClient> client = self.client;
NSURLRequest *request = self.request;
NSDictionary *headers = @{ @"Content-Type": @"image/jpeg" };
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"David_Hasselhoff.jpeg"], 1.0);
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:request.URL
statusCode:200
HTTPVersion:@"HTTP/1.1"
headerFields:headers];
[client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[client URLProtocol:self didLoadData:imageData];
[client URLProtocolDidFinishLoading:self];
}
- (void)stopLoading
{
// Must be implemented
}
@end
@suncry
Copy link

suncry commented May 30, 2016

good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment