使 UIImageView 使用解析的照片进行动画处理



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

我得到了一个urls of the photos in an array,但我无法弄清楚如何下载照片,put them in an array and then in the image view.

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

提前谢谢。

你需要使用 NSData

方法下载它们:initWithContentOfURL:options:error,然后从这个 NSData 创建一个 UIImage。

下载所有图像并具有 UIImage 数组后,请使用该数组设置 UIImageView 的 animationImages 属性。

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

要将图像从 url 添加到图像视图,解决方案是直截了当的:

imageView.image = [UIImage imageWithContentsOfURL:theURL];

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

使用此下载图像

或使用AFNetwork库链接下载图像。 下载库并将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];

}

最新更新