No visible #interface for 'UIImageView' declares the selector 'setImageWithURL - ios

Hi I have a problem with new Xcode and AFNetworking.
On xcode5 I don't have that problems but I start to make some changes and updates.
Problem is:
No visible #interface for 'UIImageView' declares the selector
'setImageWithURL
And code part:
NSString *showThumb = [[[[SettingDataClass instance] getSetting] objectForKey:#"appearance_option"] objectForKey:#"category_browse_show_thumb"];
if([showThumb isEqualToString:#"show"])
{
UIImageView *img = [[UIImageView alloc] init];
img = cell.imageView;
[cell.imageView setImageWithURL:[NSURL URLWithString:[[ToolClass instance] decodeHTMLCharacterEntities:[[aryDic objectAtIndex:indexPath.row] objectForKey:#"thumb"]]]
placeholderImage:[UIImage imageNamed:NSLocalizedString(#"image_loading_placeholder", nil)]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
img.image = [[ToolClass instance] imageByScalingAndCroppingForSize:CGSizeMake(57, 57) source:image];
}];
Where could be a problem and why this code doesn't work with Xcode6?
I will be very grateful for help

Try including "UIImageView+AFNetworking.h" in the file that uses the method.

UIImageView does not work like that. You will need to add AFNetworking's UIImageView category to get the setImageWithURL method.

Here is the apples documentation about UIImageView. As You can see there is no method like setImageWithURL.
AFNetworking has subclass of UIImageView. It has a method named setImageWithURL. If you want to use this method you need to import that class. Hope this helps.. :)
You should be writing this:
img = cell.imageView;
[cell.imageView setImageWithURL:[NSURL URLWithString:[[ToolClass instance] decodeHTMLCharacterEntities:[[aryDic objectAtIndex:indexPath.row] objectForKey:#"thumb"]]]
placeholderImage:[UIImage imageNamed:NSLocalizedString(#"image_loading_placeholder", nil)]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
img.image = [[ToolClass instance] imageByScalingAndCroppingForSize:CGSizeMake(57, 57) source:image];
}];
And your cell should have a UIImageView+AFNetworking type of imageView.

Related

How to load images using SDWebImage library

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?
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.
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"]];
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>

The UICollectionView cells are jerking while scrolling the UICollectionView

Hi all i am a newbie in IOS Development and i have a UICollection where i am loading images with SDWebImageCache. The problem is my UICollectionview cell feels like jerking or shivering while scrolling fast.
Below I post my code for loading images with SDWebImageCache
[_homeCellImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",[dictionary objectForKey:#"user_image"],[QPCommonClass cropImageWithWidth:500 andHeight:500]]] placeholderImage:[UIImage imageNamed:#"defaultIcon"]];
[_qpQuestionImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",[dictionary objectForKey:#"image"],[QPCommonClass cropImageWithWidth:1400 andHeight:1200]]] placeholderImage:[UIImage imageNamed:#"question"] options:0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
[_qpHomeActivityIndicator setHidden:YES];
[_qpHomeActivityIndicator stopAnimating];
}];
where _homeCellImage & _qpQuestionImage are UIImageViews
Make sure to load the image in a background thread.
Once the image is loaded make sure to cache it.
Now when you set the image via cellForItem, the image can be loaded directly from cache, and it should be a lot smoother.
Try to do like this--
[_homeCellImage setShowActivityIndicatorView:YES];
[_homeCellImage setIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_homeCellImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",[dictionary objectForKey:#"user_image"],[QPCommonClass cropImageWithWidth:500 andHeight:500]]] placeholderImage:[UIImage imageNamed:#"defaultIcon"] options:SDWebImageRefreshCached];
[_qpQuestionImage setShowActivityIndicatorView:YES];
[_qpQuestionImage setIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_qpQuestionImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",[dictionary objectForKey:#"image"],[QPCommonClass cropImageWithWidth:1400 andHeight:1200]]] placeholderImage:[UIImage imageNamed:#"question"] options:SDWebImageRefreshCached];
You have to add these 2 lines after your dequeue.
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

How to load an image from URL in app Extension?

I have implemented an app extension for my application, but i am facing an issue when trying to load an image from a URL into an imageView.
I tried to use PAImageView and UIImageView but both with failure.
The code that i was using for PAImageView is the following:
[self.imageView setImageURL:[NSURL URLWithString:#"https://blabblaLogo.jpg"]];
self.userImageView.clipsToBounds = YES;
and tried to use SDWebImage for UIImageView with the following:
[self.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:#"default.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
}];
and the image doesnt appear in both cases. Note that a default image from assets is displayed correctly without any issue.
Is it possible to load an image from a URL in an app Extension? and how can we achieve that?
Thank you.
Am using something like this and it's work good until this issues fixed
cell.avatar.image = [UIImage imageNamed:#"selection-box_emty.png"];
// Load the image with an GCD block executed in another thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:Arr[indexPath.row][#"avatar"]]];
if (data) {
UIImage *offersImage = [UIImage imageWithData:data];
if (offersImage) {
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *offersImage = [UIImage imageWithData:data];
cell.avatar.image = offersImage;
});
}
}
});
I faced a similar problem, put breakpoints into into library and problem apparently was App Tranport Security. We need separate App Transport Security Settings for every extension we add
Try This in your Image url
If image url contain any space it will add %20 and work fine
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

how to copy ui image to another variable?

i want to set uiimage to a variable outside the scope of function .i tried the code below but it doesn't work . code below is used in custom uitable view cell class . so i just cannot alloc and init it . as this will cause to initialize it every time cell is displayed/ scrolled
uiimage *img;
[manager downloadWithURL:[NSURL URLWithString:friendData.picUrl] options:SDWebImageRefreshCached progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
img =image; ////this is not working !!
}];
dispatch_async(dispatch_get_main_queue(), ^{
[cell.profileImage setImage:img]; //and off course this wont work too
});
I think you may just need to set __block UIImage image = nil;
You can initialize the image1 with any other initializing methods:
Try this one:
image1 = UIImage.init(CGImage:image.CGImage);
Initializer method will create new object as per this document.
I haven't tried it. Please try if it works. If not working just comment me, I may find other solution.
you need to allocate image1 to make it work out of the function.try this
image1 = [[UIImage alloc] initWithCGImage: image.CGImage];

SDWebImage setImageWithURL return empty outside any other method than cellForRowAtIndexPath

I am using SDWwbImage, wich i download from here:
https://github.com/rs/SDWebImage
I tested in the method: cellForRowAtIndexPath and everything works fine, here is my code:
[cell.image setImageWithURL:[NSURL URLWithString:object.imageUrl]];
Now i need to call an image outside this method (eg: heightForRowAtIndexPath, viewDidLoad), and i always get no image, here is my code:
UIImageView *test = [[UIImageView alloc] init];
[test setImageWithURL:[NSURL URLWithString:categories.imageUrl]];
NSLog(#"height: %f", test.image.size.height);
Is it disallowed to use setImageWithURL anywhere? how can i solve this?
EDITED
I tried to add the completion block to know when the download is finished and refresh the uitablevew, here is my code:
UIImageView *test = [[UIImageView alloc] init];
[test setImageWithURL:[NSURL URLWithString:object.imageUrl]
placeholderImage:[UIImage imageNamed:#"ic_launcher_57#2x.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
counterY++;
if (counterY == counterX) {
NSLog(#"reloading");
[self.tableView reloadData];
}
}];
But only the first time the controller is loaded, this doesnt work. Every time after this everything is fine.It looks like something cancel de download the first time.

Resources