我想为多个 UIImageViews 上的图像设置动画。图像来自服务器端的 Url,所以我请求 url 并将其转换为图像,然后我将动画提供给 uiimageview 即 crossdissolve ,发生一个问题 UIImageView 中的图像引用正在更改意味着第一次显示不同的图像,当我滚动时,其他图像显示在同一个 UIImageView 上。这个问题只会第一次发生,因为我维护图像缓存。关于这个恶毒的任何想法?下面是我使用的代码。
-(void)requestForImage:(NSString*)strURL{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSURLRequest *request;
NSString * newImageURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
request = [NSURLRequest requestWithURL:[NSURL URLWithString:newImageURL]];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
UIImage *image = [UIImage imageWithData:data];
if ([data length] > 0 && error == nil && image) //If success
{
//receive data
[[myImage sharedMyclass] setImage:image withKey:strURL];
dispatch_queue_t queue_ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue_, ^{
UIImage *image = [[myImage sharedMyclass] imageForURL:strURL]; //That return image from particular path
[self performSelectorOnMainThread:@selector(receivedImage:) withObject:image waitUntilDone:NO];
[data writeToFile:[[NSUserDefaults standardUserDefaults] valueForKey:strURL] atomically:YES];
dispatch_sync(dispatch_get_main_queue(), ^{
[UIView transitionWithView:self
duration:2.0
options: UIViewAnimationOptionTransitionCrossDissolve|UIViewAnimationOptionAllowUserInteraction
animations:^{
self.image = image;
}
completion:NULL];
self.contentMode = UIViewContentModeScaleAspectFit;
});
});
}
}];
}