before i had been pulling the same image twice from my server by using, which worked fine but i need to cut down network usage
NSString *friendAvatar = [NSString stringWithFormat:#"%#%#%#", #"http://www.mydomain.com/images/users/", myWords[0], #".jpg"];
[imageFile setImageWithURL:[NSURL URLWithString:friendAvatar]];
[bgImageFile setImageWithURL:[NSURL URLWithString:friendAvatar]]; //this is a zoomed in version of the friends photo
now i am using this way to attempt to pul the image of the UIImageView that already pulled the photo, that way i dont have to be pulling the same photo twice...
NSString *friendAvatar = [NSString stringWithFormat:#"%#%#%#", #"http://www.mydomain.com/images/users/", myWords[0], #".jpg"];
[imageFile setImageWithURL:[NSURL URLWithString:friendAvatar]];
[bgImageFile setImage:imageFile.image];
when trying to use my new method. nothing happens. no error in the debugger, the background image just simply is blank.
Based on your comment I discovered that because you are using AFNetworking when you call
[imageFile setImageWithURL:[NSURL URLWithString:friendAvatar]];
it is being executed on a background thread, but the next line
[bgImageFile setImage:imageFile.image];
is not a AFNetworking call so it is being executed before the previous line has completed and thus there is no imageFile.image to use...
So, yes, my previous answer requires you to do the async code yourself, or you could wait for the image to load before setting bgImageFile.image (Which might be done with KVO???)
Try creating a UIImage first then set UIImageView.image to the created UIImage...
UIImage *avatarImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:friendAvatar]]];
[imageFile setImage:avatarImage];
[bgImageFile setImage:avatarImage];
And a better way would be...
dispatch_queue_t myQueue = dispatch_queue_create("com.myProgram.myQueue", NULL);
dispatch_async(myQueue, ^{
UIImage *avatarImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:friendAvatar]]];
dispatch_async(dispatch_get_main_queue(), ^{
[imageFile setImage:avatarImage];
[bgImageFile setImage:avatarImage];
});
});
This will load the file from the internet on a background thread and then update the ImageViews on the main thread when the image is done loading. The benefit is that your app won't freeze up during downloading.
I hope that helps
Related
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];
NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:#"imageUrl"] ]];
cell.thumbnailImageView.image=[UIImage imageWithData:imageUrl];
this is how i use imageUrl to load them into UIImage but it takes a while to load and the program seems like it crashed or entered to an infinite loop.
How can i make the content of UIImage with url but faster?
I would suggest you to use AsyncImageView a beautiful implementation by Nicklockwood -father of iCarousel.
Link
it is very simple to use.
#import "AsyncImageView.h"
and in all imageViews do this.
[imageView setImage:#"default.png"];
[imageView setImageURL:#"whateverurl/whateverimage.png"];
In your case it would be:
[cell.thumbnailImageView setImageURL:#"yourURL"];
It works like a charm, and my code is in production. But if you still want your code to work try this:
UIActivityIndicator *activity =[[UIActivityIndicator alloc] initWithStyle:UIActivityIndicatorWhite];
[activity setFrame:CGRectMake(0,0,30,30)];
[cell.contentView addSubview:activity];
cell.thumbnailImageView.image=[UIImage imageNamed:#"Default~cell~image.png"];
dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(dispatchQueue, ^(void)
{
[activity startAnimating];
[self loadImages];
dispatch_sync(dispatch_get_main_queue(), ^{
NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:#"imageUrl"] ]];
cell.thumbnailImageView.image=[UIImage imageWithData:imageUrl]; [activity stopAnimating];
[activty setHidden:YES];
});
});
This happens because every time your cell goes off screen your image is released and if you scroll it back on screen you will have to download your image again. Simply cache your image when downloaded and check if exists use it if not download from internet. You can use third party library like SDWebImage GitHub link
Import SDWebImageView+WebCache.h and in your cellForRowAtIndexPath use the following
[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:#"imageUrl"] placeholder:[UIImage imageNamed:#"placeholder.png"]];
use sdwebimage and down load the library file from here
the few steps you do follow
add the sdwebimage in your project after that
in your .h file
#import "UIImageView+WebCache.h"
in your .m file
call the single line in your cellforRowAtIndexPath
[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:#"imageUrl"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Use an NSOperationQueue so that you can perform the loading of the data on a background thread.
After that you should set the image on the main queue.
// Assume queue is created
[queue addOperationWithBlock:^{
NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:#"imageUrl"] ]];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
cell.thumbnailImageView.image=[UIImage imageWithData:imageUrl];
}];
}];
For more about NSOperationQueue see the docs about NSOperationQueue.
So try to load image asynchronous.
https://github.com/nicklockwood/AsyncImageView
Use gcd to populate the images,
NSURL *imageUrl = [NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:#"imageUrl"] ]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
UIImage *image = nil;
image = [UIImage imageWithData: [NSData dataWithContentsOfUrl: imageUrl]];
dispatch_sync(dispatch_get_main_queue(), ^{
cell.thumbnailImageView.image = image;
});
})
You will probably want to store the image to some model object, also track if the download has been initiated, since you would not want to queue up the request for the same image url.
I have a ALAssetRepresentation question. There is a table which displays gallery images in high resolution. The fullScreenImage method takes too much time and the table works slowly. How can I implement a faster loading? For example, gallery images loading occurs immediately in the https://itunes.apple.com/us/app/print-studio-print-photos/id601882801?mt=8, how could they have done this?
ALAsset *result = photos[_index];
UIImageView *photoView = (UIImageView*)[contentView viewWithTag:PHOTO_VIEW];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
ALAssetRepresentation *represintation = [result defaultRepresentation];
NSURL* aURL = represintation.url;
[library assetForURL:aURL resultBlock:^(ALAsset *asset){
UIImage *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.3 orientation:UIImageOrientationUp];
copyOfOriginalImage = [copyOfOriginalImage imageByScalingAndCroppingForSize:CGSizeMake(600, 600)];
dispatch_async(dispatch_get_main_queue(), ^{
[photoView setImage:newImg];
[photoView setNeedsDisplay];
});
}
failureBlock:nil];
});
You can preload images before they are scrolled onto screen. The images you are preloading can be done on a background thread and saved into an NSCache instance. The cache will automatically purge (if you set the countLimit or the device runs low on memory) so when you preload, check for an item in the cache and load on a background thread if required.
I'm working with a UICollectionView and I need to load images in to the collection cell.
Here is the code
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSURL *imageURL = [NSURL URLWithString:post.cover_img_url];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
[cell.cover_img setImage:image];
});
});
post.cover_img_url = the_url_for_the_image;
cell.cover_img = the_UIImageView_object;
When I set the image not from the web, it works perfectly.
But when I try from the web, the UIImageView is blank not image.
Anyone have any ideas?
Your code looks like it should be working, but there are a couple of things that you need to test. First, you need to check to make sure that imageURL is both non-nil, and points to exactly where you think it's pointing. Copy paste a log result to your desktop browser if you have to.
NSLog(#"%#",imageURL);
Second, in the event that fetching the image fails, you need to be able to detect this. I recommend that if you continue to use the dataWithContentsOfURL: route that you at least use the following to check the error:
NSError *error = NULL;
NSData *data = [NSData dataWithContentsOfURL:imageURL options:NSDataReadingMappedIfSafe error:&error];
if (error) {
// ...
}
I am having problem with the loading of content in the app, I see that the data is fetched by the app but the images take lot of time to load, is there any possibility to load images afterwords. The code is below:
NSDictionary *dict=[discussionArray objectAtIndex:indexPath.row];
UIImageView *avatarimage = (UIImageView *)[cell viewWithTag:4];
NSString *photoStrn=[dict objectForKey:#"photo"];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSString *u=[NSString stringWithFormat:#"http://%#",photoStrn];
NSURL *imageURL=[NSURL URLWithString:u];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_sync(dispatch_get_main_queue(), ^{
UIImage *dpImage = [UIImage imageWithData:imageData];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:#"profileImage.png"];
}
avatarimage.image = dpImage;
});
If you want more details I will provide :)
You can use GCD for doing this:
dispatch_async(dispatch_get_global_queue(0,0), ^{
for (NSDictionary *dic in discussionArray)
{
NSString *photoStr=[dic objectForKey:#"photo"];
NSString * photoString=[NSString stringWithFormat:#"http://%#",photoStr];
UIImage *dpImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:#"profileImage.png"];
}
}
});
Get SDWebImage Here and add that in your project and include
UIImageView+WebCache.h
in class implementation file
UIImageView *imag=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[imag setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[[jsonarray objectAtIndex:indexPath.row]valueForKey:#"imgurl"]]] placeholderImage:[UIImage imageNamed:#"Icon#2x.png"]];
[self.view addSubview:imag];
[imag release];
SDWebImage will be more useful for loading images from URL.
Hope this Helps !!!
James,
With this line,
[NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]]
you make a synchronous network call on the main thread. The current thread will hang untile the network call is complete.
The solution would be to do an asynchronous network call. The AFNetworking library provides a great category to load images asynchronously : UIImageView+AFNetworking.