Skip to content

Instantly share code, notes, and snippets.

@chris-x-boyd
Created October 19, 2011 17:20
Show Gist options
  • Select an option

  • Save chris-x-boyd/1298991 to your computer and use it in GitHub Desktop.

Select an option

Save chris-x-boyd/1298991 to your computer and use it in GitHub Desktop.
ImageOperation: Example of a custom NSOperation.
//
// 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