3

Hi I am using SDWebImage for loading images ok that's fine

Here I am getting images from services and I want to resize those images and I want to load them using SDWebImage

for this I wrote the code below

Obj.imageYH---> this is imageURL

  NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:Obj.imageYH]];

    UIImage *actualImage = [UIImage imageWithData:imageData];
    UIGraphicsBeginImageContext(CGSizeMake(150, 150));
    [actualImage drawInRect:CGRectMake(0,0,150, 150)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *smallData = UIImagePNGRepresentation(newImage);

    NSString *Finalstring = [[NSString alloc] initWithData:smallData encoding:NSUTF8StringEncoding] ;


    [Mainimage sd_setImageWithURL:[NSURL URLWithString: Finalstring] placeholderImage:[UIImage imageNamed:@"collectionViewIcon.png"]];

but in the code above, I am getting null value in Finalstring. How can I resize it and how can I load images using SDWebImage?

api55
  • 11,070
  • 4
  • 41
  • 57
Krish
  • 4,166
  • 11
  • 58
  • 110
  • Wait. that's weird. `sd_setImageWithURL:` requires a URL of the image (e.g. http://example.com/image.jpg), instead of string representation of the image data. You should use `Obj.imageYH`. `SDWebImage` is a library to load image from Internet asynchronously. You misuse it to load a resized image. – Raptor Apr 06 '16 at 08:51

3 Answers3

2

Step 1: Installation of SDWebImage

To start with, make sure you're using the latest version of SDWebImage, and installed via CocoaPods (not recommend to link up the library manually).

Your Podfile should contain this line:

pod 'SDWebImage', '~>3.7'

After that, close the Xcode project and run pod install in your project directory. Use the Xcode workspace after installation.


Step 2: Using the codes

First of all, you have the import the library

#import <SDWebImage/UIImageView+WebCache.h>

At this moment, try to Clean & Build the Workspace. If everything goes well, the project should be built without error.

Then, in your function:

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:Obj.imageYH
                  options:0
                 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                     // progression tracking code, optional. 
                     // You can set the progress block to nil
                 }
                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                     if (image) {
                         // Resize your image here, then set it to UIImageView
                         Mainimage.image = [UIImage imageWithData:UIImagePNGRepresentation(image) scale:0.5f]; 
                         // resize to 0.5x, function available since iOS 6
                     }
                 }];

done.

Raptor
  • 53,206
  • 45
  • 230
  • 366
1
 SDWebImageManager *manager = [SDWebImageManager sharedManager];
 [manager downloadWithURL:url delegate:self options:0 success:^(UIImage
 *image)  {
     cell.profilePicture.image = [self imageByScalingAndCroppingForSize:CGSizeMake(cell.profilePicture.frame.size.width,
 cell.profilePicture.frame.size.height) image:image]; } failure:nil];

Don't forget to include

#import <SDWebImage/UIImageView+WebCache.h>
@interface YourViewController : UIViewController <SDWebImageManagerDelegate>
amit soni
  • 2,183
  • 1
  • 14
  • 17
  • #import error showing in this line – Krish Apr 06 '16 at 08:56
  • [manager downloadWithURL:url delegate:self options:0 success:^(UIImage *image) { cell.profilePicture.image = [self imageByScalingAndCroppingForSize:CGSizeMake(cell.profilePicture.frame.size.width, cell.profilePicture.frame.size.height) image:image]; } failure:nil]; – Krish Apr 06 '16 at 08:59
  • dude this is my code use your own logic and your own code then only it will work. – amit soni Apr 06 '16 at 09:00
  • [manager downloadWithURL:string delegate:self options:0 success:^(UIImage *image) { cell.profilePicture.image = [self imageByScalingAndCroppingForSize:CGSizeMake(150, 150) image:image]; } failure:nil]; – Krish Apr 06 '16 at 09:02
  • Wait. @amitsoni you're using old version of SDWebImage. Update your codes with Cocoapods. Syntax changed for months already. – Raptor Apr 06 '16 at 09:14
  • @Raptor can u give me answer please – Krish Apr 06 '16 at 09:15
1

Use below line of codes instead of your code :

UIImage *actualImage = [UIImage imageWithData:imageData];
UIGraphicsBeginImageContext(CGSizeMake(150, 150));
[actualImage drawInRect:CGRectMake(0,0,150, 150)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *smallData = UIImagePNGRepresentation(newImage);

[smallData writeToFile:Finalstring atomically:YES];// Finalstring is file path of resize image 

[Mainimage sd_setImageWithURL:[NSURL URLWithString: Finalstring] placeholderImage:[UIImage imageNamed:@"collectionViewIcon.png"]];
Payal Maniyar
  • 4,293
  • 3
  • 25
  • 51
  • That's not related to the question at all. – Raptor Apr 06 '16 at 09:13
  • @Raptor thanks for the attention. I have updated the answer. – Payal Maniyar Apr 06 '16 at 09:25
  • /how can we find Finalstring is file path of resize image ? – Krish Apr 06 '16 at 09:48
  • It is path where you want to store resize image. like in document directory. Something like below line of code `NSString *smalFilename = @"smallimage.png"; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *finalString = [documentsDirectory stringByAppendingPathComponent:smalFilename];` – Payal Maniyar Apr 06 '16 at 09:49
  • ho w can we find that path? – Krish Apr 06 '16 at 09:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108381/discussion-between-payal-maniyar-and-krish). – Payal Maniyar Apr 06 '16 at 09:52
  • @PayalManiyar this does not use the advantage of SDWebImage to download image asynchronously. – Raptor Apr 06 '16 at 09:56