Search code examples
iphoneiosmknetworkkit

How to get frame of image when using MKNetworkKit setImageFromURL?


I am trying to get the size of an image after it is downloaded using setImageFromURL:

UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[UIImageView setDefaultEngine:appDelegate.imageCache];
[img setImageFromURL:[NSURL URLWithString:@"http://testurl.com/testimg.jpg"]];
//img.frame = CGRectMake (0, 0, img.image.size.width, img.image.size.height);
[self.view addSubview:img];

I'm using MKNetworkKit -- is there a way to add a completion handler to the setImageFromURL method?


Solution

  • I was able to do it by adding this line:

     self.frame = CGRectMake(0, 0, fetchedImage.size.width, fetchedImage.size.height);
    

    to UIImageView+MKNetworkKitAdditions.m:

    if(imageCacheEngine) {
        self.imageFetchOperation = [imageCacheEngine imageAtURL:url
                       size:self.frame.size
          completionHandler:^(UIImage *fetchedImage, NSURL *url, BOOL isInCache) {
    
            self.frame = CGRectMake(0, 0, fetchedImage.size.width, fetchedImage.size.height);
    
            [UIView transitionWithView:self.superview
                              duration:isInCache?kFromCacheAnimationDuration:kFreshLoadAnimationDuration
                               options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction
                            animations:^{
                                 self.image = fetchedImage;
                               } completion:nil];
              //[[NSNotificationCenter defaultCenter] postNotificationName:KW_IMAGE_LOADED object:nil];
    
          } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
    
            DLog(@"%@", error);
          }];
      } else {
    
        DLog(@"No default engine found and imageCacheEngine parameter is null")
      }