├── .gitmodules ├── AFCoreImageResponseSerializer.podspec ├── AFCoreImageResponseSerializer ├── AFCoreImageResponseSerializer.h └── AFCoreImageResponseSerializer.m ├── LICENSE └── README.md /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "AFNetworking"] 2 | path = AFNetworking 3 | url = git@github.com:AFNetworking/AFNetworking.git 4 | -------------------------------------------------------------------------------- /AFCoreImageResponseSerializer.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = 'AFCoreImageResponseSerializer' 3 | s.version = '0.0.1' 4 | s.license = 'MIT' 5 | s.summary = 'An image response serializer for AFNetworking 2.0 that applies Core Image filters.' 6 | s.homepage = 'https://github.com/AFNetworking/AFCoreImageResponseSerializer' 7 | s.social_media_url = 'https://twitter.com/afnetworking' 8 | s.authors = { 'Mattt Thompson' => 'm@mattt.me' } 9 | s.source = { :git => 'https://github.com/AFNetworking/AFCoreImageResponseSerializer.git', :tag => '0.0.1' } 10 | s.source_files = 'AFCoreImageResponseSerializer' 11 | s.requires_arc = true 12 | 13 | s.frameworks = 'CoreImage' 14 | 15 | s.ios.deployment_target = '6.0' 16 | s.osx.deployment_target = '10.8' 17 | 18 | s.dependency 'AFNetworking', '~> 2.2' 19 | end 20 | -------------------------------------------------------------------------------- /AFCoreImageResponseSerializer/AFCoreImageResponseSerializer.h: -------------------------------------------------------------------------------- 1 | // AFCoreImageResponseSerializer.h 2 | // 3 | // Copyright (c) 2014 AFNetworking (http://afnetworking.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "AFURLResponseSerialization.h" 24 | 25 | /** 26 | `AFCoreImageResponseSerializer` is a subclass of `AFImageResponseSerializer` that applies a set of Core Image filters to response images. 27 | */ 28 | @interface AFCoreImageResponseSerializer : AFImageResponseSerializer 29 | 30 | /** 31 | The Core Image filters applied by the serializer. 32 | */ 33 | @property (readonly, nonatomic, strong) NSArray *filters; 34 | 35 | /** 36 | Creates and returns a serializer with the specified Core Image filters. 37 | */ 38 | + (instancetype)serializerWithFilters:(NSArray *)filters; 39 | 40 | /** 41 | Creates and returns a serializer with the specified Core Image filters, and `CIContext` options. 42 | 43 | @param filters The Core Image filters to be applied 44 | @param options The options passed to the `CIContext` 45 | */ 46 | + (instancetype)serializerWithFilters:(NSArray *)filters 47 | options:(NSDictionary *)options; 48 | 49 | @end 50 | -------------------------------------------------------------------------------- /AFCoreImageResponseSerializer/AFCoreImageResponseSerializer.m: -------------------------------------------------------------------------------- 1 | // AFCoreImageResponseSerializer.m 2 | // 3 | // Copyright (c) 2014 AFNetworking (http://afnetworking.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "AFCoreImageResponseSerializer.h" 24 | 25 | #import 26 | 27 | @interface AFCoreImageResponseSerializer () 28 | @property (readwrite, nonatomic, strong) CIContext *context; 29 | @property (readwrite, nonatomic, strong) NSDictionary *options; 30 | @property (readwrite, nonatomic, strong) NSArray *filters; 31 | @end 32 | 33 | @implementation AFCoreImageResponseSerializer 34 | 35 | + (instancetype)serializerWithFilters:(NSArray *)filters { 36 | return [self serializerWithFilters:filters options:nil]; 37 | } 38 | 39 | + (instancetype)serializerWithFilters:(NSArray *)filters 40 | options:(NSDictionary *)options 41 | { 42 | AFCoreImageResponseSerializer *serializer = [[self alloc] init]; 43 | serializer.filters = filters; 44 | if (options) { 45 | serializer.options = options; 46 | } 47 | 48 | return serializer; 49 | } 50 | 51 | - (id)init { 52 | self = [super init]; 53 | if (!self) { 54 | return nil; 55 | } 56 | 57 | self.options = @{kCIImageColorSpace: [NSNull null]}; 58 | 59 | return self; 60 | } 61 | 62 | - (CIContext *)context { 63 | @synchronized(self) { 64 | if (!_context) { 65 | self.context = [CIContext contextWithOptions:self.options]; 66 | } 67 | 68 | return _context; 69 | } 70 | } 71 | 72 | #pragma mark - AFURLResponseSerializer 73 | 74 | - (id)responseObjectForResponse:(NSURLResponse *)response 75 | data:(NSData *)data 76 | error:(NSError *__autoreleasing *)error 77 | { 78 | id responseObject = [super responseObjectForResponse:response data:data error:error]; 79 | if (!responseObject) { 80 | return nil; 81 | } 82 | 83 | CIImage *image = [[CIImage alloc] initWithCGImage:[responseObject CGImage]]; 84 | for (CIFilter *filter in self.filters) { 85 | [filter setValue:image forKey:kCIInputImageKey]; 86 | image = filter.outputImage; 87 | } 88 | 89 | return [UIImage imageWithCIImage:image]; 90 | } 91 | 92 | #pragma mark - NSCoding 93 | 94 | - (id)initWithCoder:(NSCoder *)aDecoder { 95 | self = [super initWithCoder:aDecoder]; 96 | if (!self) { 97 | return nil; 98 | } 99 | 100 | self.options = [aDecoder decodeObjectForKey:@"options"]; 101 | self.filters = [aDecoder decodeObjectForKey:@"filters"]; 102 | 103 | return self; 104 | } 105 | 106 | - (void)encodeWithCoder:(NSCoder *)aCoder { 107 | [super encodeWithCoder:aCoder]; 108 | 109 | [aCoder encodeObject:self.options forKey:@"options"]; 110 | [aCoder encodeObject:self.filters forKey:@"filters"]; 111 | } 112 | 113 | #pragma mark - NSCopying 114 | 115 | - (id)copyWithZone:(__unused NSZone *)zone { 116 | return [[self class] serializerWithFilters:self.filters options:self.options]; 117 | } 118 | 119 | @end 120 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 AFNetworking (http://afnetworking.com) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AFCoreImageResponseSerializer 2 | ============================= 3 | 4 | `AFCoreImageResponseSerializer` automatically applies a series of [Core Image](https://developer.apple.com/library/Mac/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_intro/ci_intro.html) filters to response images. 5 | 6 | ## Usage 7 | 8 | ```objective-c 9 | #import 10 | #import "AFCoreImageResponseSerializer.h" 11 | #import "UIImageView+AFNetworking" 12 | 13 | CIFilter *blackAndWhiteFilter = [CIFilter filterWithName:@"CIColorControls" keysAndValues:@"inputBrightness", @(0.0), @"inputContrast", @(1.1), @"inputSaturation", @(0.0), nil]; 14 | self.imageView.imageResponseSerializer = [AFCoreImageResponseSerializer serializerWithFilters:@[blackAndWhiteFilter]]; 15 | [self.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.png"]]; 16 | ``` 17 | 18 | --- 19 | 20 | ## Contact 21 | 22 | Mattt Thompson 23 | 24 | - http://github.com/mattt 25 | - http://twitter.com/mattt 26 | - m@mattt.me 27 | 28 | ## License 29 | 30 | AFCoreImageResponseSerializer is available under the MIT license. See the LICENSE file for more info. 31 | --------------------------------------------------------------------------------