मैं यह लिखा एक ऐप्लिकेशन मैं पर काम कर रहा है के लिए (अवधारणाओं और कुछ कोड लेन Roathe उत्तम UIImageView + कैश श्रेणी से लिया के साथ)। यह ASIHTTPRequest कक्षाओं का भी उपयोग करता है, जो कि बहुत अच्छे हैं। यह निश्चित रूप से सुधार किया जा सकता है .. उदाहरण के लिए, अनुरोधों को रद्द करने की अनुमति देकर, या अधिसूचना userInfo का उपयोग करके अधिक सटीक UI अद्यतन करने की अनुमति देने के लिए .. लेकिन यह मेरे उद्देश्यों के लिए अच्छा काम कर रहा है।
@implementation ImageFetcher
#define MAX_CACHED_IMAGES 20
static NSMutableDictionary* cache = nil;
+ (void)asyncImageFetch:(UIImage**)anImagePtr withURL:(NSURL*)aUrl {
if(!cache) {
cache = [[NSMutableDictionary dictionaryWithCapacity:MAX_CACHED_IMAGES] retain];
}
UIImage* newImage = [cache objectForKey:aUrl.description];
if(!newImage) { // cache miss - doh!
ASIHTTPRequest *imageRequest = [ASIHTTPRequest requestWithURL:aUrl];
imageRequest.userInfo = [NSDictionary dictionaryWithObject:[NSValue valueWithPointer:anImagePtr] forKey:@"imagePtr"];
imageRequest.delegate = self;
[imageRequest setDidFinishSelector:@selector(didReceiveImage:)];
[imageRequest setDidFailSelector:@selector(didNotReceiveImage:)];
[imageRequest startAsynchronous];
}
else { // cache hit - good!
*anImagePtr = [newImage retain];
}
}
+ (void)didReceiveImage:(ASIHTTPRequest *)request {
NSLog(@"Image data received.");
UIImage **anImagePtr = [(NSValue*)[request.userInfo objectForKey:@"imagePtr"] pointerValue];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *newImage = [[UIImage imageWithData:[request responseData]] retain];
if(!newImage) {
NSLog(@"UIImageView: LoadImage Failed");
}
else {
*anImagePtr = newImage;
// check to see if we should flush existing cached items before adding this new item
if([cache count] >= MAX_CACHED_IMAGES)
[cache removeAllObjects];
[cache setValue:newImage forKey:[request url].description];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName: @"ImageDidLoad" object: self userInfo:request.userInfo];
}
[pool drain];
}
आप इस प्रकार इस कोड को फोन: मैं भी सूचनाएं उपयोग कर रहा हूँ बेहतर या बदतर के लिए, के किसी भी मालिकों जाने के लिए
[ImageFetcher asyncImageFetch:&icon withURL:url];
इसी UIImage जब वे इस मामले में redisplay- चाहिए , यह एक tableView संदर्भ में बताया गया है:
- (void)viewDidLoad {
[super viewDidLoad];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(imageDidLoad:) name:@"ImageDidLoad" object:nil];
}
- (void)imageDidLoad:(NSNotification*)notif {
NSLog(@"Received icon load notification.");
// reload table view so that new image appears.. would be better if I could
// only reload the particular UIImageView that holds this image, oh well...
[self.tableView reloadData];
}
- (void)dealloc {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
// ...
}
स्रोत
2010-11-19 16:02:30
, तुम ग UIImageLoader (https://github.com/gngrwzrd/UIImageLoader) या SDWebImage जैसे सहायक का उपयोग करें। UIImageLoader केवल दो फाइलें और उपयोग करने के लिए सुपर सरल है। रेपो में भी एक अच्छा नमूना है। – gngrwzrd