Set UIImageView image using a url

2019-01-16 15:52发布

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

11条回答
Evening l夕情丶
2楼-- · 2019-01-16 16:29

Unfortunately this feature is not available as of this writing... instead you will have to implement the functionality yourself by:

  1. Downloading the data of the image
  2. Saving it or caching it somewhere (db or filesystem) and then
  3. Setting the UIImaveView to the saved structure

Fortunately you don't have to break your head coming out with said functionality as Apple provides an example that does exactly that as part of their code samples.

Follow the code and I'm sure you will be able to accommodate it to your needs.

查看更多
欢心
3楼-- · 2019-01-16 16:37

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]];
    });
});


}
查看更多
走好不送
4楼-- · 2019-01-16 16:37

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
查看更多
孤傲高冷的网名
5楼-- · 2019-01-16 16:43

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).

查看更多
放荡不羁爱自由
6楼-- · 2019-01-16 16:46
NSString *ImageURL = @"YourURLHere";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
imageView.image = [UIImage imageWithData:imageData];
查看更多
家丑人穷心不美
7楼-- · 2019-01-16 16:46

Here's an updated answer to the best answer, as imageWithContentsOfURL is no longer a method of UIImage. You'll have to use CIImage:

NSURL *url = [NSURL URLWithString:@"http://url_goes_here.com/logo.png"];
imageView.image = [UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:url]];
查看更多
登录 后发表回答