Reputation: 1048
UIImageview + afnetworking downloads images and caches the images.
But in certain cases the server images are = 15mb. So i need to compress them based on the some factor and make it to 1mb and then require to cache them.
SDWebImageCache on the other hand make you to define your own cache and store them
Is there any build in mechanism for downloading,editing and then later saving into the cache?
[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
{
if (image && finished)
{
// do something with image
}
}];
then use [[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey]
Is there any other alternative to doing something like this?
Upvotes: 2
Views: 283
Reputation: 13302
Your scenario with SDWebImage
is correct.
For editing purpose you need set delegate to SDWebImageManager
object and implement necessary method:
// Set delegate
[SDWebImageManager sharedManager].delegate = self;
// Implement delegate method
- (UIImage *)imageManager:(SDWebImageManager *)imageManager
transformDownloadedImage:(UIImage *)image
withURL:(NSURL *)imageURL {
UIImage scaledImage = ... // Make scale based on 'image' object
return scaledImage;
}
Note that this method called immediately after image was downloaded but before storing it to memory cache and before completion block is called.
Documentation for this method:
Allows to transform the image immediately after it has been downloaded and just before to cache it on disk and memory. NOTE: This method is called from a global queue in order to not to block the main thread.
After that you will be able to use SDWebImageDownloader
and SDImageCache
as in your question:
[SDWebImageDownloader.sharedDownloader downloadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image && finished) {
[[SDImageCache sharedImageCache] storeImage:image forKey:myCacheKey];
}
}];
Then you can manage cache by using methods of SDImageCache
class:
- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock;
- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(SDWebImageNoParamsBlock)completion;
If you need algorithm for image scaling by max data size take a look on this answer.
Upvotes: 1