UIImageJPEGRepresentation received memory warning

前端 未结 3 1232
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 12:55

I receive a memory warning when using UIImageJPEGRepresentation, is there any way to avoid this? It doesn\'t crash the app but I\'d like to avoid it if possible. It does int

相关标签:
3条回答
  • 2020-12-03 13:27

    in ARC: Just put your code inside small block of @autoreleasepool

     @autoreleasepool {
         NSData *data = UIImageJPEGRepresentation(img, 0.5);
         // something with data
    }
    
    0 讨论(0)
  • 2020-12-03 13:37

    Using UIImageJPEGRepresentation (in which you are round-tripping the asset through a UIImage) can be problematic, because using a compressionQuality of 1.0, the resulting NSData can actually be considerably larger than the original file. (Plus, you're holding a second copy of the image in the UIImage.)

    For example, I just picked a random image from my iPhone's photo library and the original asset was 1.5mb, but the NSData produced by UIImageJPEGRepresentation with a compressionQuality of 1.0 required 6.2mb. And holding the image in UIImage, itself, might take even more memory (because if uncompressed, it can require, for example, four bytes per pixel).

    Instead, you can get the original asset using the getBytes method:

    static NSInteger kBufferSize = 1024 * 10;
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        NSURL *url = info[UIImagePickerControllerReferenceURL];
    
        [self.library assetForURL:url resultBlock:^(ALAsset *asset) {
            ALAssetRepresentation *representation = [asset defaultRepresentation];
            long long remaining = representation.size;
            NSString *filename  = representation.filename;
    
            long long representationOffset = 0ll;
            NSError *error;
            NSMutableData *data = [NSMutableData data];
    
            uint8_t buffer[kBufferSize];
    
            while (remaining > 0ll) {
                NSInteger bytesRetrieved = [representation getBytes:buffer fromOffset:representationOffset length:sizeof(buffer) error:&error];
                if (bytesRetrieved <= 0) {
                    NSLog(@"failed getBytes: %@", error);
                    return;
                } else {
                    remaining -= bytesRetrieved;
                    representationOffset += bytesRetrieved;
                    [data appendBytes:buffer length:bytesRetrieved];
                }
            }
    
            // you can now use the `NSData`
    
        } failureBlock:^(NSError *error) {
            NSLog(@"assetForURL error = %@", error);
        }];
    }
    

    This avoids staging the image in a UIImage and the resulting NSData can be (for photos, anyway) considerably smaller. Note, this also has an advantage that it preserves the meta data associated with the image, too.

    By the way, while the above represents a significant memory improvement, you can probably see a more dramatic memory reduction opportunity: Specifically, rather than loading the entire asset into a NSData at one time, you can now stream the asset (subclass NSInputStream to use this getBytes routine to fetch bytes as they're needed, rather than loading the whole thing into memory at one time). There are some annoyances involved with this process (see BJ Homer's article on the topic), but if you're looking for dramatic reduction in the memory footprint, that's the way. There are a couple of approaches here (BJ's, using some staging file and streaming from that, etc.), but the key is that streaming can dramatically reduce your memory footprint.

    But by avoiding UIImage in UIImageJPEGRepresentation (which avoids the memory taken up by the image as well as the larger NSData that UIImageJPEGRepresentation yields), you might be able to make considerably headway. Also, you might want to make sure that you don't have redundant copies of this image data in memory at one time (e.g. don't load the image data into a NSData, and then build a second NSData for the HTTPBody ... see if you can do it in one fell swoop). And if worst comes to worse, you can pursue streaming approaches.

    0 讨论(0)
  • 2020-12-03 13:42

    Presented as an answer for formatting and images.

    Use instruments to check for leaks and memory loss due to retained but not leaked memory. The latter is unused memory that is still pointed to. Use Mark Generation (Heapshot) in the Allocations instrument on Instruments.

    For HowTo use Heapshot to find memory creap, see: bbum blog

    Basically the method is to run Instruments allocate tool, take a heapshot, run an iteration of your code and take another heapshot repeating 3 or 4 times. This will indicate memory that is allocated and not released during the iterations.

    To figure out the results disclose to see the individual allocations.

    If you need to see where retains, releases and autoreleases occur for an object use instruments:

    Run in instruments, in Allocations set "Record reference counts" on (For Xcode 5 and lower you have to stop recording to set the option). Cause the app to run, stop recording, drill down and you will be able to see where all retains, releases and autoreleases occurred.

    0 讨论(0)
提交回复
热议问题