【问题标题】:Make UIImageView use parsed photos to animate使 UIImageView 使用解析后的照片进行动画处理
【发布时间】:2013-10-29 12:36:41
【问题描述】:

我想使用 photos 我从 my API 解析来制作 UIImageView 动画。

我收到了urls of the photos in an array,但我不知道如何下载照片,put them in an array and then in the image view.

谁能帮我解决这个问题?我想不通:(

提前致谢。

【问题讨论】:

    标签: ios json uiimageview nsarray animated


    【解决方案1】:

    你需要使用 NSData 方法下载它们:initWithContentsOfURL:options:error,然后从这个 NSData 创建一个 UIImage。

    下载完所有图片后,你有一个 UIImage 数组,用这个数组设置 UIImageView 的 animationImages 属性。

    当然,在线程中下载图像,这样您就不会阻塞 UI。

    【讨论】:

    • +1 for 当然,在线程中下载图像,这样您就不会阻塞 UI
    • 但是我他们都有不同的网址?
    • 您需要在 for 循环中运行这些 URL,并在一个线程中下载它们中的每一个。在下载结束时将它们添加到数组中(当然是在主线程中)。当数组中的图片数量达到 url 的数量时,您就知道您下载了所有图片,然后您可以将其添加到图片视图中(同样,在主线程中)。
    【解决方案2】:

    要从 url 将图像添加到 imageview,解决方案是直接的:

    imageView.image = [UIImage imageWithContentsOfURL:theURL];
    

    对于很多图像来说,最好异步加载它们并使用图像缓存。有很多开源库,例如 SDWebImage。

    【讨论】:

      【解决方案3】:

      使用这个download images

      或使用 AFNetworking 库 link 下载图像。下载库并将 AFNetworking 文件夹添加到您的项目中。添加所需的框架(Security、MobileCoreServices 和 SystemConfiguration)并使用 AFImageRequestOperation 类。

      使用此代码下载图片

          -(void)downloadImage:(NSString *)imageUrl {
      
          // download the photo
      
          NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]];
          AFImageRequestOperation *operation = [AFImageRequestOperation
                                                imageRequestOperationWithRequest:request
                                                imageProcessingBlock:nil
                                                success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
                                                {
                                                    if (image != nil) {
                                                        NSLog(@"image downloaded %@", image);
                                                        [self.imageArray addObject:image];
                                                    }
      
                                                   //put code to add image to image view here 
                                                } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                                    NSLog(@" error %@", error.description);
      
                                                }];
      
          [operation start];
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多