Created
October 19, 2011 17:20
-
-
Save chris-x-boyd/1298991 to your computer and use it in GitHub Desktop.
ImageOperation: Example of a custom NSOperation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ImageOperation.m | |
| // AsyncImageDemo | |
| // | |
| // Created by Chris Boyd on 10/18/11. | |
| // Copyright (c) 2011 ChrisBoyd.net. All rights reserved. | |
| // | |
| #import "ImageOperation.h" | |
| @implementation ImageOperation | |
| @synthesize appDelegate, url; | |
| - (id)init { | |
| self = [super init]; | |
| if (self != nil) | |
| { | |
| self.appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; | |
| } | |
| return self; | |
| } | |
| - (void)dealloc { | |
| [url release], url = nil; | |
| [super dealloc]; | |
| } | |
| - (void)main { | |
| if (self.isCancelled) return; | |
| if (self.url == nil) return; | |
| // Create path to output images | |
| NSString *filename = [self.appDelegate sanitizedFilename:self.url]; | |
| NSString *filepath = [[self.appDelegate documentsDirectory] stringByAppendingPathComponent: | |
| [NSString stringWithFormat: @"%@", filename]]; | |
| // Download the image | |
| NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.url]]; | |
| // Check for a cancellation | |
| if (self.isCancelled) return; | |
| // Save the NSData object as a file | |
| [data writeToFile:filepath atomically:NO]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment