I am new in iOS world.I am reading tutorial on afnetworking but i have some issue while i have to fetch images from server.I follow this tutorial :-
https://www.raywenderlich.com/59255/afnetworking-2-0-tutorial
I got error when i write this code :-
[cell.imageView setImageWithURLRequest:request
placeholderImage:placeholderImage
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
error message :- no visible #interface for declares the selector setImageWithURLRequest such....
I have already import "UIImageView+AFNetworking.h"
Please help or provide me some link where i can learn fetch and uploading images to/from via Afnetworking.
You can try this one
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if (weakCell)
{
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Error: %#", error);
}];
Related
So I am working on a project and I must use AFImageDownloader in order to download some images that we need to use in our project. I use the following code:
-(void) downloadImage:(NSURL*) url
{
AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] init];
AFImageDownloader *imgDownloader = [[AFImageDownloader alloc] initWithSessionManager:sessionManager downloadPrioritization:AFImageDownloadPrioritizationFIFO maximumActiveDownloads:1 imageCache:nil];
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json",#"binary/octet-stream",nil];
sessionManager.responseSerializer = responseSerializer;
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[imgDownloader downloadImageForURLRequest:req success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *responseObject){
self.image = responseObject;
[self.delegate updateImageWithImage:self.image]; // ** CRASH **
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(#"Error");
}];
}
The delegate is of course not nil and code of the updateImageWithImage is:
-(void) updateImageWithImage:(UIImage*) img {
self.image.image = img;
}
So basically when I try to get the UIImage I get it as a response and assign it to the UICollectionViewCell and it crashes! I guess that I should do some kind of "copy" of the responseObject before using it in order parts of my program, but I am not really sure what the problem is. Any ideas?
I figured it out. I was basically using AFImagedownloader wrong, this is the proper way to use this Class:
[self.imageView setImageWithURLRequest:jpegURLRequest
placeholderImage:placeholder
success:^(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image){
NSLog(#"Success");
completionBlock(image);
}
failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
NSLog(#"ERROR!!!");
}];
I'm trying to get an image from a local url using AFNetworking with this code:
NSString *path = [DOCUMENTS stringByAppendingPathComponent:[NSString stringWithString:#"My Image.png"]];
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] placeholderImage:[UIImage imageNamed:#"Placeholder Image.png"] success:nil failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"%s: setImageWithURLRequest error: %#", __FUNCTION__, error);
}];
and I am getting this error:
NSErrorFailingURLKey=/var/mobile/Applications/6D878789-640E-4299-AA72-45D49211492D/Documents/My Image.png, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x15df8b60 "unsupported URL"}
How can I do this with AFNetworking?
Try not setting the success block to nil and use this code:
[cell.myImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"url"]] placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
weakimageView.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
//Deal with failure
}];
Try this it works perfectly fine,..
Download UIImageView+webcache.h and .m file and add to your project.
link is here https://github.com/rs/SDWebImage/blob/master/SDWebImage/UIImageView%2BWebCache.h
NSString *path = [DOCUMENTS stringByAppendingPathComponent:[NSString stringWithString:#"My Image.png"]];
[cell.imageView setImageWithURL:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] placeholderImage:[UIImage imageNamed:#"Placeholder Image.png"]];
or using Blocks,
NSString *path = [DOCUMENTS stringByAppendingPathComponent:[NSString stringWithString:#"My Image.png"]];
[cell.imageView setImageWithURL:[NSURLRequest requestWithURL:[NSURL URLWithString:path]] placeholderImage:[UIImage imageNamed:#"Placeholder Image.png"] success:^(UIImage *image, BOOL cached) {
<#code#> ---> if success
} failure:^(NSError *error) {
<#code#> ---> if failure
}];
My code is
[imageView setImageWithURL:[NSURL URLWithString:#"…"]];
to set image. Which is working fine.
Now i want to know how can i get the downloaded image from imageView. If i do imageView.image I am ending up with placeholder image. Is there any solution for this?
you have to use the following method:
[imageView setImageWithURLRequest:request
placeholderImage:placeholder
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
// the downloaded image is image
}
failure:errorHandler]
There is another method in that extension class
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
which gives you the image in success
I want to connect my iOS App with AFNetwork to my webserver with a self-signed certificate. I found a solution on github (https://github.com/AFNetworking/AFNetworking/pull/694) I tried it out and the certificate pinning seems to work but i got an other error:
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed.
(NSURLErrorDomain error -1012.)" UserInfo=0x7bc2090 {NSErrorFailingURLKey=(my domain)}
Does anybody know whether this error has to do with the AFNetworking Framework and the self signed certificate or not?
Solved:
I found the solution for the error. I had to set the SSLPinningMode to AFSSLPinningModeCertificate now it works.
AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *resDictionary = (NSDictionary *)JSON;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"%#",error);
}];
operation.SSLPinningMode = AFSSLPinningModeCertificate;
[operation start];
Try this work around.
AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *resDictionary = (NSDictionary *)JSON;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"%#",error);
}];
operation.securityPolicy.allowInvalidCertificates = YES;
[operation start];
I am trying to add a Success and Error block to check when the image is loaded so that I can use NSCache to dynamically resize the image. But when I try
[scribbleImage setImageWithURL: [NSURL URLWithString:scribble[#"scribble_image"]] placeholderImage:[UIImage imageNamed:#"Default.png"] success:^(UIImage *image) {
NSLog(#"done");
} failure:^(NSError *error) {
NSLog(#"error %#",error);
}];
Xcode gives me an error, saying No visible #interface for 'UIImageView' declares the selector 'setImageWithURL:placeholderImage:success:failure:'
I am not sure why.
PS. I am importing UIImageView+AFNetworking.h and things work just fine without a success and failure block
This is because the there is no method setImageWithURL:placeholderImage:success:failure: there is just setImageWithURLRequest:placeholderImage:success:failure:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:scribble[#"scribble_image"]]];
[scribbleImage setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:#"Default.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(#"Done");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Failed with error: %#", error);
}];
If you look at what setImageWithURL:placeholderImage: does :
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPShouldHandleCookies:NO];
[request addValue:#"image/*" forHTTPHeaderField:#"Accept"];
[self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
}
You see that this is the method is used to fetch the internally as well.