redribben

cache

Oct 26th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. - (void)secondAttempt {
  2. NSCache *memoryCache = [[NSCache alloc] init]; //assume there is a memoryCache for images or videos
  3.  
  4. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
  5.  
  6. NSString *urlString = @"http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png";
  7.  
  8. NSData *downloadedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
  9.  
  10. if (downloadedData) {
  11.  
  12. // STORE IN FILESYSTEM
  13. NSString* cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  14. NSString *file = [cachesDirectory stringByAppendingPathComponent:urlString];
  15. [downloadedData writeToFile:file atomically:YES];
  16.  
  17. // STORE IN MEMORY
  18. [memoryCache setObject:downloadedData forKey:@"downloadedPic"];
  19. }
  20.  
  21. // NOW YOU CAN CREATE AN AVASSET OR UIIMAGE FROM THE FILE OR DATA
  22. NSURL *imageURL = [NSURL fileURLWithPath:@"downloadedPic"];
  23. NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
  24. UIImage *image = [UIImage imageWithData:imageData];
  25. [scheduleImage setImage:image];
  26. });
  27. }
Advertisement
Add Comment
Please, Sign In to add comment