简体   繁体   中英

Load images in to a ScrollView

Hi have a problem with that load images (1024x1024) cached on disk to a UIScrollView takes a while (just a lagg but annoing) to load into memory. when a use images with size 668px × 445px the lagg is acceptable. I have tried to Load the data using threads same result when I use dispatch_async

Any ide how to improve preformance on loading data async?

-(void)setupImageView:(BMPhoto *)_photo {
    methodStart = [[NSDate date] retain];

    if ([[photo imageUrl] isEqualToString:[_photo imageUrl]])
        return;

     photo = _photo;

    [imageView removeFromSuperview];
    [imageView release];
    imageView = nil;

    self.zoomScale = 1.0;
    BMCache *cache = [BMCache sharedCache];


    if ([cache isFileCached:[photo imageUrl]]) {
        [NSThread detachNewThreadSelector:@selector(loadImageFromCache:) toTarget:self withObject:[photo imageUrl]];
    } else {
        [NSThread detachNewThreadSelector:@selector(loadImageFrom:) toTarget:self withObject:[photo imageUrl]];
    }
}
-(void)loadImageFromCache:(NSString *)url{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    UIImage *image = [UIImage imageWithData:[[BMCache sharedCache] getCachedRemoteFile:url]];
    [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
   [pool release];
}
-(void)loadImageFrom:(NSString *)url{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[photo imageUrl]]];
    [[BMCache sharedCache] addRemoteFileToCache:url withData:imageData];

    UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
    [imageData release];
    [self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO];
    [pool release];
}

- (void)displayImage:(UIImage *)image {
    NSDate *methodFinish1 = [NSDate date];
    NSTimeInterval executionTime1 = [methodFinish1 timeIntervalSinceDate:methodStart];
    NSLog(@"Thread Done time: %f", executionTime1);

    if (image) {
        //imageView.image = nil;
        imageView = [[BMPhotoViewImageView alloc] initWithImage:image];
        [self addSubview:imageView];

        self.contentSize = [image size];
        [self setMaxMinZoomScalesForCurrentBounds];
        self.zoomScale = self.minimumZoomScale;
        NSTimeInterval executionTime2 = [[NSDate date] timeIntervalSinceDate:methodFinish1];
        NSLog(@"Load image time time: %f", executionTime2);

        NSDate *methodFinish = [NSDate date];
        NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
        NSLog(@"Total time: %f", executionTime);
        NSLog(@"-------------------------------------------");
    }
}

Thread Done time: 0.015229 Load image time time: 0.009824

Total time: 0.028263

Thread Done time: 0.025037 Load image time time: 0.005314

Total time: 0.035781

Thread Done time: 0.026063 Load image time time: 0.004379

Total time: 0.033177

You can try by... First show the imageView(the method displayImage) put some small image states "loading" or an activity indicator. then load(use thread) and set the actual image to the imageView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM