loading images from disk in iPhone app is slow

前端 未结 3 1878
别跟我提以往
别跟我提以往 2020-12-23 12:35

In my iPhone app, I am using the iPhone\'s camera to take a photo and save it do disk (the application\'s documents folder). This is how i save it:

[UIImageJ         


        
3条回答
  •  难免孤独
    2020-12-23 13:22

    This is the faster way I know. You'll need to import #import

    I use this code to download and compress images during a scroll, inside a scrollview and you barely notice the delay.

    CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)mutableData, NULL);
    CFDictionaryRef options = (CFDictionaryRef)[[NSDictionary alloc] initWithObjectsAndKeys:(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent, (id)[NSNumber numberWithDouble:200.0], (id)kCGImageSourceThumbnailMaxPixelSize, nil];
    CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
    
    UIImage *image = [[UIImage alloc] initWithCGImage:thumbnail];
    // Cache
    NSString *fileName = @"fileName.jpg";
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"thumbnail"];
    path = [path stringByAppendingPathComponent:fileName];
    if ([UIImagePNGRepresentation(image) writeToFile:path atomically:YES]) {
        // Success
    }
    

提交回复
热议问题