title: Photo Viewer iPad & iPhone
body: <p>I've long been trying to make an image viewer, but really I do not what does not work.
  Here's a picture like you should get!</p>
  
  <p><img src="https://i.stack.imgur.com/pfwSr.png" alt="enter image description here"></p>
  
  <p>This is UIScrollView. The UIImage add to UIScrollView. When user scroll - image must download to UIImageView. I think we need download image in a separate thread. For this I am using - NSOperationqueue and NSInvocationOperation.</p>
  
  <p><strong>This is my code.</strong></p>
  
  <p>In delegate:</p>
  
  <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
      // Override point for customization after application launch.
      MyThumbnailsBook * myThumbnailsBook = [[MyThumbnailsBook alloc] initWithFrame:CGRectMake(0, 20, 768, 1004)];
      [myThumbnailsBook createThumbnails];
      [myThumbnailsBook setContentSize:CGSizeMake(768, 1004 * 10)];
      [myThumbnailsBook setBackgroundColor:[UIColor blackColor]];
      [self.window addSubview:myThumbnailsBook];
      [self.window makeKeyAndVisible];
      return YES;
  }
  </code></pre>
  
  <p><strong>In class inherit UIScrollView MyThumbnailsBook.m</strong></p>
  
  <pre><code>#define COUNT 100
  
  #import "MyThumbnailsBook.h"
  #import "MyImageDownload.h"
  
  @implementation MyThumbnailsBook
  
  
  #pragma mark -
  #pragma mark Initialization &amp; Create Thumbnails
  
  - (id)initWithFrame:(CGRect)frame{
  
      if((self = [super initWithFrame:frame])){
  
          self.delegate = self;
  
      }
  
      return self;
  }
  
  - (void)createThumbnails{
  
      float point_x = 20;
      float point_y = 20;
  
      for(NSInteger i = 0; i &lt; COUNT; i++){
  
          if(i%3==0 &amp;&amp; i != 0){
              point_x = 20;
              point_y += 220;
          }
  
          //  Create new image view.
          NSURL * url = [[NSURL alloc] initWithString:@"http://storage.casinotv.com/videos/SOYCL/pages/P68.jpg"];        
          MyImageDownload * imageDownload = [[MyImageDownload alloc] initWithImageURL:url];
          [imageDownload setFrame:CGRectMake(point_x, point_y, 200, 200)];
          [self addSubview:imageDownload];
          [imageDownload release];
          [url release];
  
          point_x += 220;
  
      }
  
  
  }
  
  #pragma mark -
  #pragma mark Scroll View Protocol
  
  - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  
  
  }
  
  
  - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
  
      // If scroll view no decelerating.
  
      if(!decelerate){
          [self asyncImageDownload];
      }
  
  }
  
  
  - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ 
  
      [self asyncImageDownload];
  
  }
  
  
  #pragma mark -
  #pragma mark Download Image
  
  //  If scroll stopped - download image to thumbnails.
  
  - (void)asyncImageDownload{
  
  
  }
  
  
  @end
  </code></pre>
  
  <p>In class inherit UIScrollView MyImageDownload.m</p>
  
  <pre><code>#import "MyImageDownload.h"
  
  
  @implementation MyImageDownload
  
  //  This is init method. When class init - we add new operation for download image.
  
  - (id)initWithImageURL:(NSURL*)url{
  
      if((self == [super init])){
  
          [self setBackgroundColor:[UIColor yellowColor]];        
  
          NSArray * params = [NSArray arrayWithObjects:url, nil];
          queue = [[NSOperationQueue alloc] init];
          [queue setMaxConcurrentOperationCount:1]; 
          NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(asyncDownload:) object:params];           
          [queue addOperation:operation];        
          [params release];             
  
      }
  
      return self;
  }
  
  
  //  Download image to data and after to self (UIImageView).
  
  - (void)asyncDownload:(NSArray *)params{
  
  
      NSData * data = [NSData dataWithContentsOfURL:[params objectAtIndex:0]]; //  Get image data with url.
  
      if(data) [self setImage:[UIImage imageWithData:data]];
  
      [data release]; 
  
  }
  
  @end
  </code></pre>
  
  <p>In this example, the images are not loaded and I do not understand why?
  I have always used these classes in which the images were loaded from the internet, but I also had problems with memory and I do not know too how to solve it. For example, when to scroll images I get exception - <strong>Received memory warning. Level=1 &amp; 2</strong> and after app crash. And I have no idea what to do. I understand what you need as a separate download of images over time and to remove non visible objects but I have found I need the algorithm.</p>
  
  <p>For example - when i go to scrollDidScroll method i get all object when i not see them and remove image:</p>
  
  <pre><code>NSArray *views = [self subviews];
      for (UIImageView *v in views) {
          if(v.frame.origin.y &gt;= self.contentOffset.y &amp;&amp; v.frame.origin.y &lt;= self.contentOffset.y + self.frame.size.height){
  
              //  If image of imageview is equal nil - call new operation
              if(v.image == nil){             
                  [self requestImageForIndexPath:[NSNumber numberWithInt:v.tag]];                               
              }
              else
              {
                  v.image = nil;
              }
  
          }
      }
  </code></pre>
  
  <ul>
  <li>self - this is class inherit UIScrollView.</li>
  </ul>
  
  <p>I was confused and ask for help in resolving this issue. TNX all!</p>