Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在后台下载图像以在iOS中设置imageView?_Ios_Objective C_Uiimageview - Fatal编程技术网

如何在后台下载图像以在iOS中设置imageView?

如何在后台下载图像以在iOS中设置imageView?,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,我使用了下面的代码,但不适用于我 UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(markerView.bounds.size.width/10, markerView.bounds.size.height/4, markerView.bounds.size.width*4/5, markerView.bounds.size.height/1.5)]; NSString *strPath = [NSStr

我使用了下面的代码,但不适用于我

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(markerView.bounds.size.width/10, markerView.bounds.size.height/4, markerView.bounds.size.width*4/5, markerView.bounds.size.height/1.5)];

NSString *strPath = [NSString stringWithFormat:@"%@",[[arrTemp valueForKey:@"image"] objectAtIndex:marker.accessibilityLabel.integerValue]];
NSLog(@"img path %@", strPath);

dispatch_async(dispatch_get_main_queue(), ^{
  UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]]];
  imgView.image = img;
});
        NSString *str = [NSString stringWithFormat:@"%@",[[[dictTemp valueForKey:@"image"] objectAtIndex:j] valueForKey:@"imagepath"]];
        NSString *strSub = [str stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        NSURL *imgUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",strSub]];

        dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(q, ^{
            /* Fetch the image from the server... */
            NSData *data = [NSData dataWithContentsOfURL:imgUrl];
            UIImage *img = [[UIImage alloc] initWithData:data];

            dispatch_async(dispatch_get_main_queue(), ^{

                [imageView setImage:img];
            });
        });
您可以尝试下面的代码。 这对我有用

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(markerView.bounds.size.width/10, markerView.bounds.size.height/4, markerView.bounds.size.width*4/5, markerView.bounds.size.height/1.5)];

NSString *strPath = [NSString stringWithFormat:@"%@",[[arrTemp valueForKey:@"image"] objectAtIndex:marker.accessibilityLabel.integerValue]];
NSLog(@"img path %@", strPath);

dispatch_async(dispatch_get_main_queue(), ^{
  UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]]];
  imgView.image = img;
});
        NSString *str = [NSString stringWithFormat:@"%@",[[[dictTemp valueForKey:@"image"] objectAtIndex:j] valueForKey:@"imagepath"]];
        NSString *strSub = [str stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        NSURL *imgUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",strSub]];

        dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(q, ^{
            /* Fetch the image from the server... */
            NSData *data = [NSData dataWithContentsOfURL:imgUrl];
            UIImage *img = [[UIImage alloc] initWithData:data];

            dispatch_async(dispatch_get_main_queue(), ^{

                [imageView setImage:img];
            });
        });
您可以尝试下面的代码。 这对我有用

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(markerView.bounds.size.width/10, markerView.bounds.size.height/4, markerView.bounds.size.width*4/5, markerView.bounds.size.height/1.5)];

NSString *strPath = [NSString stringWithFormat:@"%@",[[arrTemp valueForKey:@"image"] objectAtIndex:marker.accessibilityLabel.integerValue]];
NSLog(@"img path %@", strPath);

dispatch_async(dispatch_get_main_queue(), ^{
  UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strPath]]];
  imgView.image = img;
});
        NSString *str = [NSString stringWithFormat:@"%@",[[[dictTemp valueForKey:@"image"] objectAtIndex:j] valueForKey:@"imagepath"]];
        NSString *strSub = [str stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        NSURL *imgUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",strSub]];

        dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(q, ^{
            /* Fetch the image from the server... */
            NSData *data = [NSData dataWithContentsOfURL:imgUrl];
            UIImage *img = [[UIImage alloc] initWithData:data];

            dispatch_async(dispatch_get_main_queue(), ^{

                [imageView setImage:img];
            });
        });

在项目中添加
SDWebImage
文件夹,然后选择以下选项。将文件夹副本添加到目标项目并创建组

然后像这样调用header

#import "UIImageView+WebCache.h"
并确保添加到要使用该库的所有目标

文件位于:

将SDWebImage项目添加到项目中

  • 从下载页面下载并解压缩框架的最新版本
  • 右键单击项目导航器并选择“将文件添加到您的项目”:
  • 在对话框中,选择SDWebImage.framework:
  • 选中“将项目复制到目标组的文件夹(如果需要)”复选框
或者你也可以按照其他答案的建议使用
可可豆

终于打电话来了

[imgView sd_setImageWithURL:[NSURL URLWithString:strPath] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

在项目中添加
SDWebImage
文件夹,然后选择以下选项。将文件夹副本添加到目标项目并创建组

然后像这样调用header

#import "UIImageView+WebCache.h"
并确保添加到要使用该库的所有目标

文件位于:

将SDWebImage项目添加到项目中

  • 从下载页面下载并解压缩框架的最新版本
  • 右键单击项目导航器并选择“将文件添加到您的项目”:
  • 在对话框中,选择SDWebImage.framework:
  • 选中“将项目复制到目标组的文件夹(如果需要)”复选框
或者你也可以按照其他答案的建议使用
可可豆

终于打电话来了

[imgView sd_setImageWithURL:[NSURL URLWithString:strPath] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

您可以尝试DispatchQueue.async(xcode 8.0+):

或者将第一个
DispatchQueue
替换为
URLSession.dataTask

let imageURL: URL = URL(string: "https://www.brightedge.com/blog/wp-content/uploads/2014/08/Google-Secure-Search_SEL.jpg")!

(URLSession(configuration: URLSessionConfiguration.default)).dataTask(with: imageURL, completionHandler: { (imageData, response, error) in

    if let data = imageData {
        DispatchQueue.main.async {
            self.imageView.image = UIImage(data: data)
        }
    }
}).resume()

您可以尝试DispatchQueue.async(xcode 8.0+):

或者将第一个
DispatchQueue
替换为
URLSession.dataTask

let imageURL: URL = URL(string: "https://www.brightedge.com/blog/wp-content/uploads/2014/08/Google-Secure-Search_SEL.jpg")!

(URLSession(configuration: URLSessionConfiguration.default)).dataTask(with: imageURL, completionHandler: { (imageData, response, error) in

    if let data = imageData {
        DispatchQueue.main.async {
            self.imageView.image = UIImage(data: data)
        }
    }
}).resume()