Re: Invalidating UIImageView's image cache
Re: Invalidating UIImageView's image cache
- Subject: Re: Invalidating UIImageView's image cache
- From: Carl Hoefs via Cocoa-dev <email@hidden>
- Date: Wed, 23 Sep 2020 12:04:06 -0700
Yes! +[UIImage imageNamed:] was The Caching Culprit™️. I was barking up the
wrong object (UIImageView).
As per Eric's suggestion I've switch to +[UIImage imageWithContentsOfFile:],
and now there's no image caching (as per Mike's suggestion to RTM: "This
method does not cache the image object". I simply didn't notice/expect the docs
would comment on that).
Plus my code is much cleaner now as I can stop the flail of uniquing the
filenames, deleting the sandbox files, etc...
Thanks Eric & Mike!
-Carl
> On Sep 23, 2020, at 1:34 AM, Mike Abdullah <email@hidden> wrote:
>
> Correct, this is your issue. Have a read of the docs on +[UIImage
> imageNamed:] They explicitly discuss the cache. This is not the API you
> want.
>
> Mike.
>
>> On 23 Sep 2020, at 02:12, Eric Lee via Cocoa-dev <email@hidden>
>> wrote:
>>
>> Ah maybe it is the use of `imageNamed:`. I believe that caches the image
>> data in a system cache. Have you tried `imageWithContentsOfFile:`?
>>
>> https://developer.apple.com/documentation/uikit/uiimage/1624123-imagewithcontentsoffile
>>
>>> On Sep 22, 2020, at 16:56, Carl Hoefs <email@hidden>
>>> wrote:
>>>
>>>
>>>> On Sep 22, 2020, at 1:46 PM, Eric Lee via Cocoa-dev
>>>> <email@hidden> wrote:
>>>>
>>>>> I don't have a good answer, but I think this may be more that UIImage
>>>>> caches the images, not UIImageView. Maybe you can find something in
>>>>> UIImage's docs/headers?
>>>>
>>>> I think you may be on to something. This WWDC
>>>> <https://developer.apple.com/videos/play/wwdc2018/219> session covers
>>>> optimizing UIImage performance and has some info on what UIImage caches.
>>>>
>>>> How are you creating the UIImage? Are you retaining the UIImage anywhere
>>>> outside of the UIImageView?
>>>
>>>
>>> The path of UIImage creation is as follows:
>>>
>>> - For each manually-initiated processing pass of the app, a standard set of
>>> 12 JPG files is written to the sandbox using the OpenCV::imwrite()
>>> function, which creates a JPG from data values. The files have fixed
>>> filenames.
>>>
>>> - When all 12 JPG files for a pass are written, I then use the following
>>> code snippet to create the UIImages and display them on the main
>>> UIImageView:
>>>
>>> NSMutableArray *uiImagesArray = [NSMutableArray new];
>>> for (NSString *file in [[NSFileManager defaultManager]
>>> contentsOfDirectoryAtPath:self.sandboxPath error:NULL]) {
>>> if ([file.pathExtension isEqualToString:@"jpg"]) {
>>> UIImage *tempImage = [UIImage imageNamed:jpgFilename];
>>> if (tempImage) [uiImagesArray addObject:tempImage];
>>> }
>>> }
>>> UIImage *allAGFAImages = [UIImage animatedImageWithImages:uiImagesArray
>>> duration:20.0];
>>> self.imageView.image = allAGFAImages;
>>>
>>> - When a new pass of the app is run, the sandbox contents are deleted (see
>>> below), the uiImagesArray variable is set to nil, and the new JPG files are
>>> written, using the same filenames as before. Note that I do not set each
>>> UIImage in the array explicitly to nil.
>>>
>>> [[NSFileManager defaultManager] removeItemAtURL:[NSURL
>>> fileURLWithPath:[self.sandboxPath stringByAppendingPathComponent:file]]
>>> error:&error];
>>>
>>> - If I add a uniquing string to the filenames for each pass, the problem
>>> does not present itself. Otherwise the original (old) cached image contents
>>> are displayed until the app is restarted.
>>>
>>> - I don't retain the UIImages anywhere other than adding them to the array
>>> in the code snippet above.
>>>
>>> -Carl
>>>
>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden