UIImage view show previous image not update next one.this is my code
[userImage setImageWithURL:[NSURL URLWithString:[Engin shared].imagePath] placeholderImage:[UIImage imageNamed:#"dummyImge.png"]];
when i come first time on this view where i added this code in my viewdidload function ok first time image update successfully and after that i update a new image on server and get path of that image after uploaded on server and save him a separate class like that
[Engin shared].imagePath = [JSON valueForKey:#"image"];
and after that i go back to previous view and again come on that view and run this line again
[userImage setImageWithURL:[NSURL URLWithString:[Engin shared].imagePath] placeholderImage:[UIImage imageNamed:#"dummyImge.png"]];
but now this not update the image rather they show the previous image . when i check path of that image they show updated (mean) show new image but when i call function which image path they now previous image
NSUrlRequset *request = [NSUrlRequest requestWithURL:[NSURL URLWithString:[Engin shared].imagePath]];
[userImage setImageWithURLRequest:request
placeholderImage:[UIImage imageNamed:#"dummyImge.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
//handle success message here
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
//handle error message here
}];
i did not actually run this code , but you can test it your self.
Related
I am using this code for displaying the image from URL to UIImageview
UIImageView *myview=[[UIImageView alloc]init];
myview.frame = CGRectMake(50, 50, 320, 480);
NSURL *imgURL=[[NSURL alloc]initWithString:#"http://soccerlens.com/files/2011/03/chelsea-1112-home.png"];
NSData *imgdata=[[NSData alloc]initWithContentsOfURL:imgURL];
UIImage *image=[[UIImage alloc]initWithData:imgdata];
myview.image=image;
[self.view addSubview:myview];
But the problem is that its taking too long time to display the image in imageview.
Please help me...
Is there any method to fast the process...
Instead of dispatch_async, Use SDWebImage for caching the images.
This is best I have seen...
The problem of dispatch_async is that if you lost focus from image, it will load again. However SDWebImage, Caches the image and it wont reload again.
The answers given to me on my own question Understanding the behaviour of [NSData dataWithContentsOfURL:URL] inside the GCD block does makes sense.So be sure that if you use [NSData dataWithContentsOfURL:URL] inside the GCD(as many developers do these days) is not a great idea to download the files/images.So i am leaning towards the below approach(you can either use NSOperationQueue).
Load your images using [NSURLConnection sendAsynchronousRequest:queue:completionHandler: then use NSCache to prevent downloading the same image again and again.
As suggested by many developers go for SDWebimage and it does include the above strategy to download the images files .You can load as many images you want and the same URL won't be downloaded several times as per the author of the code
EDIT:
Example on [NSURLConnection sendAsynchronousRequest:queue:completionHandler:
NSURL *url = [NSURL URLWithString:#"your_URL"];
NSURLRequest *myUrlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:myUrlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil)
//doSomething With The data
else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
//time out error
else if (error != nil)
//download error
}];
Use Dispatch queue to load image from URL.
dispatch_async(dispatch_get_main_queue(), ^{
});
Or add a placeholder image till your image gets load from URL.
I am getting strange issue please if get help from anyone then it'll be appreciated.
My problem is i am getting only Image names from server through web service for ex: abc.png.
Now that image need to be attached with the static URL & than it will be used to display to UIImageView.
I have used this code:
for (int i = 0; i<[[myDic valueForKey:#"posts"] count]; i++)
{
NSData *bgImageData = nil;
if ([[[[myDic valueForKey:#"posts"] objectAtIndex:i] valueForKey:#"post"] valueForKey:#"video_image"] == [NSNull null])
{
bgImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://drupal.org/files/issues/no-pre.png"]];
}
else
{
bgImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://sitebildz.com/application/document/video/%#",[[[[myDic valueForKey:#"posts"] objectAtIndex:i] valueForKey:#"post"] valueForKey:#"video_image"]]]];
}
[arrVideoImage addObject:bgImageData];
}
For Display in Table :
cell.imageView.image = [UIImage imageWithData:[arrVideoImage objectAtIndex:indexPath.row]];
But don't know why its not working.
Thanks.
As commented The Tiger, your best choice would be to use an external library like SDWebImage which can handle download, caching and display of online images.
Here is an example using this framework :
[imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
You can also use blocks to insert logic once an image finished loading :
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];
You're doing wrong. See you're making NSData in loop. Do you think it will wait until image is download from server ??? Nope it will not wait. So in this case you're adding empty NSData.
Would be better to use SDWebImage.
1. Download it
2. Import it in your project
3. build project (success)
4. Import #import "UIImageView+WebCache.h" in your ViewController class
[cell.imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"placeHolderImageName"]];
you cab use https://github.com/enormego/EGOImageLoading/tree/master/EGOImageView which will download the image from server in async way and display once it completely downloaded from server. It also cache that image so that multiple request can be avoided and it enhances the system.
I have a UICollectionViewCell, in which it has a UIImageView abd I am using the AFNetworking UIImageView category to help me load the images. I've profiled my app and it seems that the memory spike is over the top, it just keeps increasing and increasing after profiling it via Instruments under allocation tools.
I am not sure what happened here and how to fix this. Cells are being reused all the time, but it's just that maybe these images aren't purged from the cache or something. Here's some code of how I am using it, it's pretty straightforward:
NSURLRequest *imageURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[self.imageView_ setImageWithURLRequest:imageURLRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakSelf.imageView_.image = image;
weakSelf.imageView_.alpha = 0.0;
[UIView animateWithDuration:0.3 animations:^{
weakSelf.imageView_.alpha = 1.0;
}];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
}];
If I remove this code to download the image, then the memory footprint is way smaller. Any idea on what might be causing this?
Try to wrap it in autoreleasepool and check your memory usage
#autoreleasepool {
// Code that creates autoreleased objects.
}
Um using the SDWeb image Here
and these my Code :
UIImageView *sellerImage = [[UIImageView alloc]init];
[sellerImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:productPicture]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
but it always load the placeholder image only not the image from URL,
I've tried to use blocks to make sure if it load's the image or not and it enter to the Success Block which means that SDWebImage can load the image from URL but it doesn't change the place holder image.
These is the code When I tried to use blocks :
[sellerImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:productPicture]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
success:^(UIImage *image, BOOL cached)
{
NSLog(#"success Block");
}
failure:^(NSError *error)
{
NSLog(#"failure Block");
}];
As I said it always goes in the success Block which means it can load the image from URL but it doesn't change the place holder image.
and these is a screen from my problem.
also try the following
[sellerImage setImageWithURL:[NSURL URLWithString:[productPicture stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
success:^(UIImage *image, BOOL cached)
{
dispatch_async(dispatch_get_main_queue(), ^{
sellerImage.image = image;
});
NSLog(#"success Block");
}
failure:^(NSError *error)
{
NSLog(#"failure Block");
}];
try the following
UIImageView *sellerImage = [[UIImageView alloc]init];
[sellerImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:
[productPicture stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Assign a placeholder image properly.. issue with me was with wrong image extension that doesn't match with each other i.e. placeholder.jpeg and palceholder#2x.jpg
I'm looking to set the image within a custom tableviewcell and use the height of the returned image within the heightForRowAtIndexPath method.
Is there anyway to get this returned data into the heightForRowAtIndexPath method?
Currently I'm using a local dictionary and setting it as per below:
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:recent.show.showImage] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[cell.imageView setImageWithURLRequest:urlRequest placeholderImage:[UIImage imageNamed:#"icon"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSNumber *imageHeight = [[NSNumber alloc] initWithFloat:image.size.height];
NSString *indexString = [NSString stringWithFormat:#"%d", indexPath.row];
[self.returnedImages setValue:imageHeight forKey:indexString];
[imageHeight release];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
}];
any pointers on a more efficient approach greatly appreciated.
Cheers
Nik
The way you're currently doing this in a NSDictionary is appropriate. Although to make sure it updates as quickly as possible you could add [self.tableView reloadData] to your success block, since then it will immediately refresh your tableview with the updated data. Even better you could use the reloadRowsAtIndexPaths:withRowAnimation: and pass it just the index path of the newly downloaded UIImage so it won't refresh the entire tableview.
Docs