Set UIImageView image using a url

后端 未结 10 690
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 02:24

Is it possible to read a url to an image and set a UIImageView to the image at this url?

相关标签:
10条回答
  • 2020-12-08 02:45

    If you want to display image from Url in ImageView, And also want to save this image in cache for optimize to server interaction this would help you Just pass your imageView object and string Url to this function

    -(void)downloadingServerImageFromUrl:(UIImageView*)imgView AndUrl:(NSString*)strUrl{
    
    
    strUrl = [strUrl encodeUrl];
    
    NSString* theFileName = [NSString stringWithFormat:@"%@.png",[[strUrl lastPathComponent] stringByDeletingPathExtension]];
    
    
    NSFileManager *fileManager =[NSFileManager defaultManager];
    NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]];
    
    
    
    imgView.backgroundColor = [UIColor darkGrayColor];
    UIActivityIndicatorView *actView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [imgView addSubview:actView];
    [actView startAnimating];
    CGSize boundsSize = imgView.bounds.size;
    CGRect frameToCenter = actView.frame;
    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;
    
    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;
    
    actView.frame = frameToCenter;
    
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
    
        NSData *dataFromFile = nil;
        NSData *dataFromUrl = nil;
    
        dataFromFile = [fileManager contentsAtPath:fileName];
        if(dataFromFile==nil){
            dataFromUrl=[[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strUrl]] autorelease];                      
        }
    
        dispatch_sync(dispatch_get_main_queue(), ^{
    
            if(dataFromFile!=nil){
                imgView.image = [UIImage imageWithData:dataFromFile];
            }else if(dataFromUrl!=nil){
                imgView.image = [UIImage imageWithData:dataFromUrl];  
                NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]];
    
                BOOL filecreationSuccess = [fileManager createFileAtPath:fileName contents:dataFromUrl attributes:nil];       
                if(filecreationSuccess == NO){
                    NSLog(@"Failed to create the html file");
                }
    
            }else{
                imgView.image = [UIImage imageNamed:@"NO_Image.png"];  
            }
            [actView removeFromSuperview];
            [actView release];
            [imgView setBackgroundColor:[UIColor clearColor]];
        });
    });
    
    
    }
    
    0 讨论(0)
  • 2020-12-08 02:52

    EGOImageLoading

    EGOImageView* imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];
    imageView.frame = CGRectMake(0.0f, 0.0f, 36.0f, 36.0f);
    
    //show the placeholder image instantly
    [self.anyView addSubview:imageView];
    [imageView release] //if you want
    
    //load the image from url asynchronously with caching automagically
    imageView.imageURL = [NSURL URLWithString:photoURL]; 
    

    If you want more, there is a delegate to handle actions after loading

    @protocol EGOImageViewDelegate<NSObject>
    @optional
    - (void)imageViewLoadedImage:(EGOImageView*)imageView;
    - (void)imageViewFailedToLoadImage:(EGOImageView*)imageView error:(NSError*)error;
    @end
    
    0 讨论(0)
  • 2020-12-08 02:52

    Another option is TTImageView from the Three20 library which handles asynchronous loading of an image. The control works great. However, the downside is you have to deal with the nightmare of installing Three20 (which is then almost impossible to fully remove).

    0 讨论(0)
  • 2020-12-08 02:55

    It's possible to load an NSData from a URL and convert that to an image and stick that in the image view, but this is an extremely bad idea because it involves doing a synchronous URL download on the main thread. This will lock up the UI and possibly cause the user to think your app has crashed if the image doesn't download extremely fast.

    Edit: To clarify, the original question looks to me like the poster wants to say something along the lines of

    imageView.image = [UIImage imageWithContentsOfURL:theURL];
    

    Hence my answer. It's possible to do the equivalent by going through NSData, but it's a bad idea. Instead, the image should be downloaded asynchronously using NSURLConnection, and only once it's fully downloaded should it be converted into a UIImage and assigned to the image view.

    0 讨论(0)
  • 2020-12-08 02:59
    NSString *ImageURL = @"YourURLHere";
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
    imageView.image = [UIImage imageWithData:imageData];
    
    0 讨论(0)
  • 2020-12-08 03:02

    I am using https://github.com/rs/SDWebImage which is a beautifully designed library, which has the options to put a placeholder image, whether to memory, or disk cache the image or use the downloader independent of a UIImageView.

    I was trying to do that by myself but the library took all the pain away.

    All you need to do is to write the code below for your case :

    #import "UIImageView+WebCache.h"
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    

    Worked like a charm for me.

    0 讨论(0)
提交回复
热议问题