I have an image which I'm loading in a UIWebView. I've set the scaleToFit as YES for webView. Still it doesn't scale to the screen of webView.
This is happening only when I'm running it on iOS 10.3.1. It work fine on older versions.
self.webView.scalesPageToFit = YES;
Have you placed any constraints on the image? It could be to do with the device size rather than the iOS version?
Just a thought, might be completely wrong...
May I ask why you load an image into a UIWebView in the first place?
I recommend using UIImageView whenever possible. With this, you can set it's "contentMode" to your preferred setting. If you need a way to load remote images, check out this really popular & easy to use library:
https://github.com/rs/SDWebImage
With the above library loading a remote image is as easy as this:
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:iconURL] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
[yourImageView setImage:image];
});
}];
Related
I tried to load an image progressively in my app. Basically what I tried is while the image is loading, I want to show the fully loaded image from blurred state. I tried,
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:[NSURL URLWithString:#"https://profile.microsoft.com/RegsysProfileCenter/Images/personal_info.jpg"]
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
[self.profileBtn setImage:[UIImage imageWithData:[NSData dataWithBytes:&receivedSize length:sizeof(receivedSize)] scale:15] forState:UIControlStateNormal];
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
// [self.profileBtn setImage:image forState:UIControlStateNormal];
}
}];
Is it possible to load an image progressively using SDWebImage. Please help me with this.
You can try this implementation
+ (void)loadImageWithURLString:(NSString *)urlString forImageView:(UIImageView *)imageView {
[imageView sd_setImageWithURL:[Utilities stringToURL:urlString]
placeholderImage:[UIImage placeHolderImage]
options:SDWebImageProgressiveDownload];
}
The option says that
/**
* This flag enables progressive download, the image is displayed progressively during download as a browser would do.
* By default, the image is only displayed once completely downloaded.
*/
SDWebImageProgressiveDownload = 1 << 3,
You can show percentage of downloading on temp label or below activity indicator (can show activityindicator on image view or button till download is not completed) like in progress block,
NSInteger downloadCompleted = (receivedSize/ expectedSize) * 100;
label.text = downloadCompleted; //temp label on image view or below activityindicator
Hope this will help :)
Anyway to handle or display UIImage with caching and with placeholder?
I have found one answer from stack exchange but it didnot work for me given below the link :
Best way to cache images on ios app?
Notes: I dont have any URL because i get image object of PHIMAGE asset library.
You can use SDWebImage
It is very simple to display and cache image.
[imageView sd_setImageWithURL:[NSURL URLWithString:#"http://www.image_url.com"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
You can use a very usefull library called SDWebImage
SDWebImage automagically cache all your images when you provide a valid URL with the sd_setImageWithURL. So the next time you call it, will use the cache system instead of re-download the image.
Example to implement in a UIImageView from their github:
// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
There was a problem of smoothing an all image while scroll the table but there is an issue of reload the cell not the caching problem i solved this issue.
I get an image one by one from local identifier which is provided by PHAsset framework and display that image into cell.
For reference Code:
PHFetchResult *savedAssets = [PHAsset fetchAssetsWithLocalIdentifiers:#[your local identifier] options:nil];
[savedAssets enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop) {
//this gets called for every asset from its localIdentifier you saved
PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.synchronous = YES;
imageRequestOptions.deliveryMode = PHImageRequestOptionsResizeModeFast;
[[PHImageManager defaultManager]requestImageForAsset:asset targetSize:CGSizeMake(50,50) contentMode:PHImageContentModeAspectFill options:imageRequestOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSLog(#"You get an image from result");
}];
}];
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];
I'm loading the images from URL in UITableView. But it's very slow when loading an view. Here's an example,
UIImage *image = nil;
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://calcuttans.com/palki/wp-content/uploads/2009/02/kidscover-small.png"]]];
In Table view, UIButton i'm setting the background image.
Please Can you provide the sample.
FYI : I'm used the LazzyTable sample program but it's not much helpful. Can you suggest any other samples.
Load image asynchronously
NSURL* url = [NSURL URLWithString:#"http://calcuttans.com/palki/wp-content/uploads/2009/02/kidscover-small.png"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (!error){
NSImage* image = [[NSImage alloc] initWithData:data];
// do whatever you want with image
}
}];
There are some open source libraries available for this:
HJCache
SDWebImage
These libraries download image in a asynchronous manner and cache it for further use.
Try to implement AFNetworking. It uses async requests to download the image, you are currently blocking your view with every download.
You can then use an AFImageRequestOperation to download your image.
if you load the image all download from the internet every time , it must be very slow.
I think you shuold exist your download image to the filePath , and when you will load the image , you can check whether the image has been downloaded before , if not ,then download. if it has been downloaded , you can use imageWithContentsOfFile: method to load the image
//Make use of dispatch queue for faster processing of data. add this in viewDidLoad
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSData * data=[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
[self performSelectorOnMainThread:#selector(setImage:) withObject:data waitUntilDone:YES];
});
//once data is got set the image and reload tableview
-(void)setImage:(NSData *)responseData
{
image = [UIImage imageWithData:responseData];
[tableView reloadData];
}
maybe you can use asihttprequest to lazy load images. use ASINetworkQueues
You've to use NSOperationQueue to make your tableview efficient.
Check this icodeblog tutorial and raywenderlich tutorial
Have a look at this tutorial. It helped me a lot. When I was using it I was quite new to iOS in general and it was helpful not only with respect to loading images from the web.
http://www.markj.net/iphone-asynchronous-table-image/
With AFNetworking it is more easy.
//AFNetworking
#import "UIImageView+AFNetworking.h"
[cell.iboImageView setImageWithURL:[NSURL URLWithString:server.imagen] placeholderImage:[UIImage imageNamed:#"qhacer_logo.png"]];
I'm using AFNetworking's UIImageView+AFNetworking.h to asynchronously load an online image onto a UIImageView.
The remote image loads very well when I use setImageWithURLRequest:, but when I try using setImageWithURLRequest:placeholderImage:success:failure the remote image is downloaded but the UIImageView does not set it as its UIImage.
Here's my code:
#property (strong) IBOutlet UIImageView *imageView;
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[_imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://i.imgur.com/fVhhR.png"]]
placeholderImage:nil
success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image ){
NSLog(#"Loaded successfully: %d", [response statusCode]);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(#"failed loading: %#", error);
}
];
}
Image always loads successfully, the status code is always 200. But I have to add [_imageView setImage:image]; to the success block for the UIImageView to set the image.
I'm using iOS 6.0
Any ideas?
If you supply a success block then you are responsible for setting the image property of your UIImageView. If you supply a nil success block then AFNetworking does it for you.
This is the relevant section of UIImageView+AFNetworking.m (line 117):
if (success) {
success(operation.request, operation.response, responseObject);
} else {
self.image = responseObject;
}
Alternatively, just use the -setImageWithURL:placeholderImage: without the completion blocks.
I had the same problem and after being frustrated with the docs for the library I found what Robin mentioned above. So now I'm using:
[imageView setImageWithURLRequest:imageRequest placeholderImage:nil success:nil failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Unable to retrieve image");
}];
Works like a charm. I hope this helps.
The doc is very clear:
If a success block is specified, it is the responsibility of the block
to set the image of the image view before returning. If no success
block is specified, the default behavior of setting the image with
self.image = image is applied.
So you can either pass a nil block for success (as mentioned above) or call setImage:image yourself if you specify a block
In your success block, you need to give the UIImaage *image to your UIImageView. I assuming you are getting the NSLog(#"Loaded successfully: %d", [response statusCode]);
Also, it appears your placeholder image is nil. Don't you want a placeholder like a loading image in there?
For those who has stumbled upon this issue and got here while browsing and searching on the web, here's an another reason - the image might have wrong file extension. For example, the image might be saved as jpg, but someone has decided to change its file extension to png.
Here's how to check that: first you need to use your network monitoring tool (for example Charlie) and check image request "Content-Type" HTTP header. Suppose, it has value "image/png". This means the image on the backend has png file extension. Now, run your app on simulator, then go to simulator folder of your app:
/Users/you/Library/Application Support/iPhone simulator/select-folder-with-your-iOS-version/Applications/YourApp/Library/Caches/yourAppBundleId/fsCachedData/
Find your image file and add png or jpg extension to this file (AFNetworking caches image files without file extension, so need to add it in order to open it with Preview). Open it with Preview and press Tools/Show Inspector. Check for "Document Type" in the opened panel. If it's different than "PNG image" (for example JPEG image) then that's the cause and you need to fix the image itself on the backend - open it with appropriate image editing program and save it as PNG.