ios - How to turn GIF image into lightweight UIImage? -
I am working on a GIF viewer but I have a problem with thumbnail images, they are not animated, but when I have a huge GIF As an image, see if it brings a lot of time and interface.
This is my method how I do this:
- (UICollectionViewCell *) View the collection: (UICollectionView *) collectionView cellForItemAtIndexPath: {NSIndexPath *} Index {MyCell * MyCell = [collectionView dequeueReusableCellWithReuseIdentifier: @ "cell" forIndexPath: indexPath]; UIImage * image; NSDTa * Data = [NSDTATA DATAVITY content of URL: selvedateosource [indexpath.ro]]; Image = [UIImage imageWithData: data]; MyCell.myImageView.image = image; Make me return; }
The URL is local and as I know it is to set a very large GIF (some megabytes each) because the UIs are expensive and it takes time.
How do I optimize it? I want to create non-animated images with light thumbnails from image GIF data. How do I do this?
Edit: Actually I've implemented the cache to store them, but this is not the ideal solution because I have to clean the cache sometimes with memory warnings. My question is still valid. I have a code for now:
- (UICollectionViewCell *) View the collection: (UICollectionView *) View the collectionSailForItemActWindowspath: (NSIndexPath *) IndexPath {MyCell * myCell = [collectionView dequeueReusableCellWithReuseIdentifier: @ " Cell "indexpath: indexpath]; UIImage * image; If ([self.cache objectFor: SwadeshData source [indexPath.row]]) {image = [UIImage imageWithData: [self.cache objectForKey: self.dataSource [indexPath.row]]]; } Other {NSDT * Data = [NSData DatyWid content of URL: selvedateosource [indexpath.ro]]; Image = [UIImage imageWithData: data]; } MyCell.myImageView.image = image; Make me return; }
To quickly display thumbnails, you need to create a thumbnail of the image This is required when it is downloaded and caches to disk. In the case of an animated GIF, you probably want to hold the first frame, scale it until it is only a few kilobytes, then cache it on disk and then display it. Something like this (Uncheckered code):
NSData * ImageData ... ... // wherever you get your image data UIImage * originalImage = [UIImage imageWithData: imageData]; CGSize original size = original image size; CGSize scaleFactor = 0.25f; // whatever scale you want CGSize newSize = CGSizeMake (original size. Width * scale, original size, hect * scale factor); UIGraphicsBeginImageContext (newSize); CGRF Newframe = CGRTZero; NewFrame.size = newSize; [Original image drawInRect: newFrame]; UIImage * shrunkImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); NSDT * pngDataToBeCached = UIImagePNG report (shrunkImage);
Loading the resulting PNG should be very fast. To make sure that they do not get backup in iTunes, save them in the cache folder Also keep in mind that the scaling and caching process of images can be on the background thread to avoid the UI.
Comments
Post a Comment