ImageIO_PNG_Data memory usage is high

Asked 1 years ago, Updated 1 years ago, 72 views

Within the app, you can select images from the camera roll to display them in the collection view.

However, it consumes a lot of memory when displaying images.
I found out that imageIO_PNG_Data was the cause of the investigation in Instruments, but I don't know how to fix it.

Instruments

//Launch Camera Roll
- (void) showCameraRoll
{
    UIImagePickerControllerSourceTypesourceType
    = UIImagePickerControllerSourceTypePhotoLibrary;
    if([UIImagePickerController isSourceTypeAvailable:sourceType]) {
        UIImagePickerController*picker=[UIImagePickerController alloc] init;
        picker.sourceType=sourceType;
        picker.delegate=self;
        [self presentViewController:picker animated:YES completion:NULL];
    }
}


// Processing after image selection from camera roll
- (void) imagePickerController: (UIImagePickerController*)picker didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    NSLog (@ "After Image Selection");
    UIImage* originalImage=(UIImage*) [ info objectForKey: UIImagePickerControllerOriginalImage];
    UIImage*editImage=(UIImage*) [ info objectForKey: UIImagePickerControllerEditedImage];

    UIImage* saveImage;

    if(editImage){
        saveImage=editImage;
    } else{
        saveImage=originalImage;
    }

    // Save camera roll only for photos taken with camera
    UIImageWriteToSavedPhotoAlbum (editImage, nil, nil, nil);

    [self addSelectedPicture:self item:saveImage]
    [picker dismissViewControllerAnimated:YES completion:nil];
    self.collectionView reloadData;
}


// Image preservation processing
+ (void) saveLessons: (NSArray*)lessons
{
    NSData*data=[NSKeyedArchiver archivedDataWithRootObject:lessons];  
    NSSstring* path = [self makeLibraryPath];
    data writeToFile:path atomically:YES;
}

+ (NSString*) makeLibraryPath
{
    NSArray* paths = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES);
    NSSstring*dir = [paths objectAtIndex:0];
    return [dir stringByAppendingPathComponent:@"data.dat";
}

Please let me know if anyone knows anything.
Thank you for your cooperation.

objective-c iphone

2022-09-29 21:38

1 Answers

Displaying the image you got in the collection view doesn't mean it doesn't have to be a full-size image, but a thumbnail of about 100 x 100 pixels at most, right? If you have a full-size image, I think it's reasonable to consume memory.To reduce memory consumption, you may need to write a program to create a smaller image for the collection view, but the following sample code is thumbnailed using Photo Framework introduced by iOS 8.
Only the UIImagePickerController Delete method is listed.Import #import<Photos/Photos.h> to use the Photos Framework.

-(void) imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString*,id>*)info{
    // Retrieved the reference URL for the selected image in the image picker.
    NSURL* imageURL=(NSURL*) [ info objectForKey: UIImagePickerControllerReferenceURL ];
    // Retrieved the asset asset for the image.
    PHFetchResult*fetchResult= [PHAsset fetchAssetsWithALAssetURLs: [NSArray arrayWithObject:imageURL] options:nil];
    PHAsset*asset=fetchResult.firstObject;
    // Requested to generate and return an image of the specified size (72x72).
    [PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(72.0,72.0)contentMode:PHImageContentModeAspectFitoptions:nil
        resultHandler:^(UIImage* image, NSDictionary* info) {
            // Write the program that uses the acquired image here.
    }];

    [self-dismissViewControllerAnimated: YES completion: NULL]
}


2022-09-29 21:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.