【问题标题】:setImageWithUrl is not showing the image at firstimesetImageWithUrl 没有第一次显示图像
【发布时间】:2015-02-09 19:58:21
【问题描述】:

我的应用程序显示了很多图像,因此我使用页面控件在单个控制器中显示图像。 用户可以通过滑动查看所有图像。

我正在使用 AFNetworking 库来展示良好的性能。我使用了setImageWithUrl的属性

// This is myCode
NSURL *myUrl = [NSURL URLWithString:@"http://mdb.scicloudsolutions.com:8001/sites/default/files/landscape_design_1.jpg"];
[imageView setImageWithUrl:myUrl];

它只在第一次显示.. 当我打印网址和图片时

NSLog(@"Url is .. : %@ image. is : %@",myUrl,imageView.image);

回应: 我收到 url 值但不是图像值。 第一次显示图片为空。

如何解决?

【问题讨论】:

  • 最初加载图像需要时间,因为它是从 url 下载的。在图像下载或图像为零时放置一个占位符图像。也将此代码放在后台线程中。
  • @Kampai 感谢您的回复,根据您的回复,我已修改它在我的应用程序启动时显示占位符图像,当我滑动我的页面时,它仅显示原始 url 图像。如果不滑动页面,则仅显示占位符图像。如何解决这个问题。

标签: ios ios6 uiimage afnetworking


【解决方案1】:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    /*============= YOU ARE ON BACK GROUND THREAD ==================*/

     UIImageView *yourImageView=[[UIImageView alloc] init];
     UIImage *img=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"pass your url here"]]];


    dispatch_async(dispatch_get_main_queue(), ^(void) {
        /*============= YOU ARE ON MAIN THREAD ==================*/
        yourImageView.image=img;
    });
});

【讨论】:

  • 以字符串格式传递您的网址
【解决方案2】:

为此使用以下解决方案:

@property (nonatomic, strong) NSMutableData *data
@property (nonatomic, strong) NSURLConnection *connection;



NSURLRequest *request = [NSURLRequest requestWithURL:url 
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                     timeoutInterval:60.0];

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

将这些委托方法添加到您的视图控制器:

- (void)connection:(NSURLConnection *)connection 
    didReceiveData:(NSData *)incrementalData {
    if (data==nil) {
        data = [[NSMutableData alloc] initWithCapacity:2048];
    }
    [data appendData:incrementalData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
    connection = nil;

    UIImage *image = [UIImage imageWithData:data];
    // Set above image to your imageView.

    data = nil;
}

【讨论】:

    【解决方案3】:

    Here You Set [imageView setImageWithUrl:myUrl]; 因此将图像加载到 ImageView 需要时间。

    使用SDWebImage 库更好。只需简单地从链接下载此库并将图像设置为

    [imageView sd_setImageWithURL:url
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    

    【讨论】:

    • 默认是占位符图片,一旦我在屏幕上做了任何操作,就会出现url图片
    • @Jyoshna 第一次需要时间来下载图像然后在它直接从缓存内存中获取之后你也可以通过'github.com/rs/SDWebImage'找到这个问题这个链接仔细阅读它。
    猜你喜欢
    • 2014-09-13
    • 1970-01-01
    • 2016-06-23
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    相关资源
    最近更新 更多