XMImageDownloader.m

//
//  XMImageDownloader.m
//  XMRemoteImage
//
//  Created by Alex Clarke on 27/02/09.
//  Copyright 2009 CocoaLab Software Australia. All rights reserved.
//
#import "XMImageDownloader.h"
@implementation XMImageDownloader
- (id) initWithProxyImagePath:(NSString *)proxyPath remoteImagePath:(NSString *)remotePath targetKey:(NSString *)key {
	self = [super init];
	if (self != nil) {
		[self setTargetKey:key];
		NSURL * proxyImageURL =  [NSURL fileURLWithPath:proxyPath isDirectory:NO];
		NSURL * remoteImageURL =  [NSURL URLWithString:remotePath];
		NSImage *proxyImage = [[[NSImage alloc] initWithContentsOfURL:proxyImageURL] autorelease];
		if (proxyImage != nil) {
			[self setImage:proxyImage];
		}
		NSImage *remoteImage = [[[NSImage alloc] initByReferencingURL:remoteImageURL] autorelease];
		[NSThread detachNewThreadSelector: @selector(startLoading:) toTarget:self withObject:remoteImage]; 
	}
	return self;
}
- (void) dealloc
{
	targetKey = nil;
	[targetKey release];
	image = nil;
	[image release];
	[super dealloc];
}
- (void) startLoading:(NSImage *)anImage { 
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
	if ([anImage isValid]) {
		[self performSelectorOnMainThread:@selector(imageReady:) withObject:anImage waitUntilDone:NO];
	}
	[pool release];
}
- (void) imageReady:(NSImage *)anImage {
	NSMutableDictionary * userInfo = [NSMutableDictionary dictionary];
	[userInfo setValue:[self targetKey] forKey:kXMTargetKey];
	[self setImage:anImage];
	[[NSNotificationCenter defaultCenter] postNotificationName:kXMImageDownloadedNotification object:anImage userInfo:userInfo];
} 
- (NSImage *)image {
	return [[image retain] autorelease];
}
- (void)setImage:(NSImage *)value {
	if (image != value) {
		[image release];
		image = [value copy];
	}
}
- (NSString *)targetKey {
    return [[targetKey retain] autorelease];
}
- (void)setTargetKey:(NSString *)value {
    if (targetKey != value) {
        [targetKey release];
        targetKey = [value copy];
    }
}
@end
 

Post new comment

The content of this field is kept private and will not be shown publicly.
Enter the code shown in the image:

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Donate!





If you like what you find here and wish to support further development of this site, please donate via PayPal. No account required.

Syndicate

Syndicate content

User login

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
10 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Enter the code shown in the image: