CIImage with CGImage memory grow up - ios

I'm creating a photo slideshow app.
My app flow :
user select photo assets > (over 100+)
load images from assets > (display image size)
set imageView image or add CIFilter to image and slides
Question :
When I create CIImage Object from CGImage , the memory is growing up so fast and if the image count over 100+, the app crash.
But it's strange that I remove the create code, the app works fine.
Can anybody help me ?
more code :
- (void)loadDisplayImagesWithCompletion:(void (^)(NSArray *images))completion {
dispatch_async(dispatch_queue_create("PhotosEditViewController_loadImageQueue", nil), ^{
__block NSMutableArray *images = [NSMutableArray array];
__block int handleCount = 0;
__weak PhotosEditViewController *weakSelf = self;
for (PHAsset *asset in self.photoAssets) {
[KPPhotoManager requestImageForAsset:asset targetSize:self.contentView.frame.size completeBlock:^(UIImage *image) {
[images addObject:image];
#autoreleasepool {
CIImage *ciImage = [CIImage imageWithCGImage:[image CGImage]];//memory growing up
}
}];
}
});
}
// KPPhotoManager
+ (void)requestImageForAsset:(PHAsset*)asset targetSize:(CGSize)size completeBlock:(void (^)(UIImage *image))completeBlock
{
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeAspectFill options:[self createImageRequestOptions] resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
completeBlock(result);
}];
}

I created a new project copying your code. I increased the number of calls to 10000 and inspecting the memory all looks well. It does jump a bit but does not inflate.
I also tried to force image load using:
for (int i=0; i<10000; i++) {
#autoreleasepool {
UIImage *image = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"image2.png"]];
[self createFilterImageWithImage:image];
}
}
and it still does not inflate the memory.
How are you inspecting your memory leak? Are you looking at the memory consumption? Are there any specifics in your project or file such as having disabled ARC?

Related

Very slow to load images from PHAsset

I am using following code to fetch images and AVAsset from PHAsset. Here are two arrays in code :
galleryArr : to store images for collection view.
mutableDataArr : store images (for image asset) and videos (for AVAsset) to upload on server
Its very slow to fetch all images from PHAssets array.
I googled about this, most of people says remove this line [options setSynchronous:YES]; but if I remove this line then completion is called twice and array duplicates the objects (as objects are appended in array within completion).
for (int i = 0; i < assets.count; i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.deliveryMode = PHImageRequestOptionsDeliveryModeOpportunistic;
options.resizeMode = PHImageRequestOptionsResizeModeExact;
[options setNetworkAccessAllowed:YES];
[options setSynchronous:YES];
PHImageManager *manager = PHImageManager.defaultManager;
PHVideoRequestOptions *videoOptions = [[PHVideoRequestOptions alloc] init];
videoOptions.networkAccessAllowed = YES;
__weak typeof(self) weakSelf = self;
if (assets[i].mediaType == PHAssetMediaTypeVideo) {
[manager requestAVAssetForVideo:[assets objectAtIndex:i] options:videoOptions resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
if ([asset isKindOfClass:[AVURLAsset class]])
{
[weakSelf.mutableDataArr addObject:asset];
}
}];
}
[manager requestImageForAsset:[assets objectAtIndex:i]
targetSize: CGSizeMake(1024, 1024) //PHImageManagerMaximumSize
contentMode:PHImageContentModeAspectFit
options:options
resultHandler:^(UIImage *image, NSDictionary *info) {
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
if (assets[i].mediaType != PHAssetMediaTypeVideo) {
[weakSelf.mutableDataArr addObject:image];
}
[galleryArr addObject:image];
if (i+1 == assets.count) {
[SVProgressHUD dismiss];
[weakSelf.galleryCollectionView reloadData];
}
});
}
}];
});
}
Any suggestion please?
Just one thought, it looks like you are loading all the images from the array before removing your progress HUD and displaying the gallery. As the number of images could be very large and presuming you are using a collection view or similar, that's quite an overhead before anything is displayed.
I did something like this a while ago and instead of looping through the array and loading everything up front, I let the cells request images as they needed them. This makes it very fast and efficient as cells can display immediately with a loading icon, then flip to the image when it was available. Efficiency comes from only loading images the user is actually going to see.
To make things performant, and by performant I mean I could scroll as fast as I liked without the display freezing, each cell would first check an in memory cache for the image, then trigger a request for an image on a background thread.
When the image was returned, the cell would add it to the in memory cache and then if the cell had not being reused for a different image (due to fast scrolling) it would display the image.
Further, I also used a NSCache for the in memory cache so that if the app started to use a lot of memory, images would be automatically dropped and reloaded the next time a cell wanted one.
The summary is to use a memory aware cache, and only load what you actually need.

Memory issue while fetching images from Photos library with metadata

I'm trying to get all photos from photos library with image's metadata. It works fine for 10-20 images but when there are 50+ images it occupies too much memory, which causes to app crash.
Why i need all images into array?
Answer - to send images to server app. [i'm using GCDAsyncSocket to send data on receiver socket/port and i don't have that much waiting time to request images from PHAsset while sending images on socket/port.
My Code :
+(void)getPhotosDataFromCamera:(void(^)(NSMutableArray *arrImageData))completionHandler
{
[PhotosManager checkPhotosPermission:^(bool granted)
{
if (granted)
{
NSMutableArray *arrImageData = [NSMutableArray new];
NSArray *arrImages=[[NSArray alloc] init];
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
NSLog(#"%d",(int)result.count);
arrImages = [result copy];
//--- If no images.
if (arrImages.count <= 0)
{
completionHandler(nil);
return ;
}
__block int index = 1;
__block BOOL isDone = false;
for (PHAsset *asset in arrImages)
{
[PhotosManager requestMetadata:asset withCompletionBlock:^(UIImage *image, NSDictionary *metadata)
{
#autoreleasepool
{
NSData *imageData = metadata?[PhotosManager addExif:image metaData:metadata]:UIImageJPEGRepresentation(image, 1.0f);
if (imageData != nil)
{
[arrImageData addObject:imageData];
NSLog(#"Adding images :%i",index);
//--- Done adding all images.
if (index == arrImages.count)
{
isDone = true;
NSLog(#"Done adding all images with info!!");
completionHandler(arrImageData);
}
index++;
}
}
}];
}
}
else
{
completionHandler(nil);
}
}];
}
typedef void (^PHAssetMetadataBlock)(UIImage *image,NSDictionary *metadata);
+(void)requestMetadata:(PHAsset *)asset withCompletionBlock:(PHAssetMetadataBlock)completionBlock
{
PHContentEditingInputRequestOptions *editOptions = [[PHContentEditingInputRequestOptions alloc]init];
editOptions.networkAccessAllowed = YES;
[asset requestContentEditingInputWithOptions:editOptions completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info)
{
CIImage *CGimage = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];
UIImage *image = contentEditingInput.displaySizeImage;
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(image,CGimage.properties);
});
CGimage = nil;
image = nil;
}];
editOptions = nil;
asset =nil;
}
+ (NSData *)addExif:(UIImage*)toImage metaData:(NSDictionary *)container
{
NSData *imageData = UIImageJPEGRepresentation(toImage, 1.0f);
// create an imagesourceref
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData, NULL);
// this is the type of image (e.g., public.jpeg)
CFStringRef UTI = CGImageSourceGetType(source);
// create a new data object and write the new image into it
NSMutableData *dest_data = [[NSMutableData alloc] initWithLength:imageData.length+2000];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)dest_data, UTI, 1, NULL);
if (!destination) {
NSLog(#"Error: Could not create image destination");
}
// add the image contained in the image source to the destination, overidding the old metadata with our modified metadata
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef) container);
BOOL success = NO;
success = CGImageDestinationFinalize(destination);
if (!success) {
NSLog(#"Error: Could not create data from image destination");
}
CFRelease(destination);
CFRelease(source);
imageData = nil;
source = nil;
destination = nil;
return dest_data;
}
Well it's not a surprise that you arrive into this situation, since each of your image consumes memory and you instantiate and keep them in memory. This is not really a correct design approach.
In the end it depends on what you want to do with those images.
What I would suggest is that you keep just the array of your PHAsset objects and request the image only on demand.
Like if you want to represent those images into a tableView/collectionView, perform the call to
[PhotosManager requestMetadata:asset withCompletionBlock:^(UIImage *image, NSDictionary *metadata)
directly in the particular method. This way you won't drain the device memory.
There simply is not enough memory on the phone to load all of the images into the photo library into memory at the same time.
If you want to display the images, then only fetch the images that you need for immediate display. For the rest keep just he PHAsset. Make sure to discard the images when you don't need them any more.
If you need thumbnails, then fetch only the thumbnails that you need.
If want to do something with all of the images - like add a watermark to them or process them in some way - then process each image one at a time in a queue.
I cannot advise further as your question doesn't state why you need all of the images.

Generating video thumbnails of all videos from gallery gives memory issues

I am working on an application where I need to show the thumbnails of all the videos in my gallery(viewed as collection view).Now I am using AVAssetImageGenerator to generate the thumbnails from videos in gallery but I am getting memory issues.Here is my code that I am using:
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];
PHImageManager *imageManager = [PHImageManager new];
for(NSInteger i=0 ; i < fetchResult.count ; i++){
__weak SAVideosViewController *weakSelf = self;
[imageManager requestAVAssetForVideo:fetchResult[i] options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
[collectionViewData addObject:asset];
//my method to generate video thumbnail...
[self generateThumbnailForAsset:asset];
if(i == fetchResult.count-1){
collectionViewDataFilled = YES;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.myCollectionView reloadData];
});
}
}];
}
Here is the method called above:
-(void)generateThumbnailForAsset:(AVAsset*)asset_{
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset_];
CMTime time = CMTimeMakeWithSeconds(1,1);
CMTimeShow(time);
CGImageRef img = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
if(img != nil){
NSLog(#"image");
[thumbnails addObject:[UIImage imageWithCGImage:img]];
}
CGImageRelease(img);
}
I want to know why I'm getting memory issues here and how can I resolve it.
Seems like no one knows the correct answer . So here I am answering my own question after some research .
NOTE : To do such a thing,you do not need to use AVAssetImageGenerator.You the following methods instead(PHOTOS FRAMEWORK).
PHCachingImageManager *cachingImageManager;
[cachingImageManager startCachingImagesForAssets:collectionViewData targetSize:cellSize contentMode:PHImageContentModeAspectFill options:nil];
The class used here is PHCachingImageManager.Read about this in apple docs.
Then after this,use this second method to retrieve data from cache.
[cachingImageManager requestImageForAsset:collectionViewData[indexPath.row] targetSize:AssetGridThumbnailSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
cell.myImageView.image = result;
}];
The callback handler gives an image that you can use in your collection View cells.use this in cellForItemAtIndexPath method.
For a complete code and reference,refer to this example by APPLE.
https://developer.apple.com/library/ios/samplecode/UsingPhotosFramework/Introduction/Intro.html

Memory issue when requesting for Gallery image DATA using Photos Framework in iOS

I am working on an iOS app in which I have to fetch all the images from iPhone gallery and then fetch its exif Value.To get exif value I need to get image using image data.
I am using following code for this:
-(void)getExifDataFromImage:(NSInteger)index{
PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.synchronous=YES;
options.resizeMode=PHImageRequestOptionsResizeModeFast;
[[PHImageManager defaultManager] requestImageDataForAsset:asset
options:options
resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info)
{
CIImage* ciImage = [CIImage imageWithData:imageData];
NSLog(#"Metadata : %#", ciImage.properties);
exifCount++;
NSDictionary *pro=ciImage.properties;
NSDictionary *exifDic=[pro objectForKey:#"{Exif}"];
NSDictionary *tiffDic=[pro objectForKey:#"{TIFF}"];
}];}
All works fine for the images around 1200 but if we go beyond that we get memory issue and the application crashes.
I also tried getting image using the below code to get the image and then tried to find the exif value of the resized image.
[self.imageManager requestImageForAsset:asset targetSize:CGSizeMake(360, 360) contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage *result, NSDictionary *info)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self doSomething]
dispatch_async(dispatch_get_main_queue(), ^{
imageView.image=result;
});//
});
}];
But I am unable to get the exif value for the resized images.
Error : libBacktraceRecording.dylib: allocate_free_list_pages() -- virtual memory exhausted!
Please suggest me how to handle this Memory crash issue. Also please suggest if there is another way to get the exif value.
You're loading autoreleased images with CIImage* ciImage = [CIImage imageWithData:imageData];. That will use up a lot of data really quickly because the Autorealeasepool is not drained.
You can wrap the image creation and dictionary access in #autoreleasepool{/* your code*/}. Or just use CIImage* ciImage = [[CIImage alloc] initWithData:imageData]; to let ARC take care of of freeing the memory.

High memory usage looping through PHAssets and calling requestImageForAsset

I'm using an image picker library to allow the user to select many images from their photo library. They are returned as an array of PHAssets. Then, I want to convert all the PHAssets to UIImages and write them to the app's storage.
At the moment, I'm looping through all the assets and calling requestImageForAsset synchronously. My issue is that there is incredibly high memory usage spike when this loop is being run (with 30 images, it spikes up to 130MB). I would like to prevent this.
Here is my code:
for(PHAsset *asset in self.assets) {
NSLog(#"started requesting image %i", i);
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:[self imageRequestOptions] resultHandler:^(UIImage *image, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
assetCount++;
NSError *error = [info objectForKey:PHImageErrorKey];
if (error) NSLog(#"Image request error: %#",error);
else {
NSString *imagePath = [appDelegate.docsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%i.png",i]];
NSData *imageData = UIImagePNGRepresentation(image);
if(imageData) {
[imageData writeToFile:imagePath atomically:YES];
[self.imagesArray addObject:imagePath];
}
else {
NSLog(#"Couldn't write image data to file.");
}
[self checkAddComplete];
NSLog(#"finished requesting image %i", i);
}
});
}];
i++;
}
Based on the logs, I see that all of the "starting requesting image x" are called first, then all of the completion blocks ("finished requesting image x"). I think that this might be contributing to the memory issue. It would probably be less memory intensive to ensure that the completion block for each iteration is called before freeing those resources and moving to the next iteration. How can I do this?
#Inder Kumar Rathore trick does not work for me.
So i tried read more about PHImageManager here
I found that if i switch from
- requestImageForAsset:targetSize:contentMode:options:resultHandler:
to
- requestImageDataForAsset:options:resultHandler:
i will receive the image with the same dimension {5376, 2688} but the size in byte is much smaller. So the memory issue is solved.
hope this help !!
(note : [UIImage imageWithData:imageData] use this to convert NSData to UIImage)
Please use autoreleasepool for memory management.
for(PHAsset *asset in self.assets) {
// This autorelease pool seems good (a1)
#autoreleasepool {
NSLog(#"started requesting image %i", i);
[[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:[self imageRequestOptions] resultHandler:^(UIImage *image, NSDictionary *info) {
dispatch_async(dispatch_get_main_queue(), ^{
//you can add autorelease pool here as well (a2)
#autoreleasepool {
assetCount++;
NSError *error = [info objectForKey:PHImageErrorKey];
if (error) NSLog(#"Image request error: %#",error);
else {
NSString *imagePath = [appDelegate.docsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%i.png",i]];
NSData *imageData = UIImagePNGRepresentation(image);
if(imageData) {
[imageData writeToFile:imagePath atomically:YES];
[self.imagesArray addObject:imagePath];
}
else {
NSLog(#"Couldn't write image data to file.");
}
[self checkAddComplete];
NSLog(#"finished requesting image %i", i);
}
} //a2 ends here
});
}];
i++;
} // a1 ends here
}
I solved it with a recursive method that ensures that the completion block is finished before next iteration. This way it is possible to get thousands of photos with very low memory.
Here is what it does:
Given an array of indices of selected photos in the camera roll
Request first object in array.
In completion block, remove first object in array and call same method again.
Repeat until array is empty
Here is the code.
- (void)processPhotos
{
NSIndexPath *indexPath = [_selectedPhotosArray objectAtIndex:0];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
PHAsset *asset = [_allPhotos objectAtIndex:indexPath.row];
[self.imageManager requestImageForAsset:asset
targetSize:PHImageManagerMaximumSize
contentMode:PHImageContentModeAspectFill
options:self.imageRequestOptions
resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSError *error = [info objectForKey:PHImageErrorKey];
if (_selectedPhotosArray.count > 0) {
[_selectedPhotosArray removeObjectAtIndex:0];
}
if (error) {
NSLog(#"[CameraRoll] Image request error: %#",error);
} else {
if (result != nil) {
[self processImage:result];
}
}
if (_selectedPhotosArray.count > 0) {
// Recurring loop
[self processPhotos];
}
}];
});
}
Make sure to check if array not empty before calling this method for the first time.

Resources