2013-03-10 30 views

回答

0

您可以使用AFNetWorking從網站加載圖像。 像這樣更改MosaicDataView.m中的方法setModule

-(void)setModule:(MosaicData *)newModule 
{ 
module = newModule; 

__weak UIImageView *weakImage = imageView; 
__weak UIView *weakView = self; 
__weak MosaicData *weakModule = module; 

UIImage *image = [imageView loadImage:module.tg_id ofType:@"png"]; 
if (image) 
{ 
    [imageView setImage:image]; 
} 
else 
{ 
    [imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:module.l_cover_url]] placeholderImage:[UIImage imageNamed:@"test.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
    { 
     [weakImage setImage:image]; 
     CGSize imgFinalSize = CGSizeZero; 

     [weakImage saveImage:image withFileName:weakModule.tg_id ofType:@"png"]; 

     if (image.size.width < image.size.height) 
     { 
      imgFinalSize.width = weakView.bounds.size.width; 
      imgFinalSize.height = weakView.bounds.size.width * image.size.height/image.size.width; 

      // This is to avoid black bars on the bottom and top of the image 
      // Happens when images have its height lesser than its bounds 
      if (imgFinalSize.height < weakView.bounds.size.height) 
      { 
       imgFinalSize.width = weakView.bounds.size.height * weakView.bounds.size.width/imgFinalSize.height; 
       imgFinalSize.height = weakView.bounds.size.height; 
      } 
     } 
     else 
     { 
      imgFinalSize.height = weakView.bounds.size.height; 
      imgFinalSize.width = weakView.bounds.size.height * image.size.width/image.size.height; 

      // This is to avoid black bars on the left and right of the image 
      // Happens when images have its width lesser than its bounds 
      if (imgFinalSize.width < weakView.bounds.size.width) 
      { 
       imgFinalSize.height = weakView.bounds.size.height * weakView.bounds.size.width/imgFinalSize.height; 
       imgFinalSize.width = weakView.bounds.size.width; 
      } 
     } 

     weakImage.frame = CGRectMake(0, 0, imgFinalSize.width, imgFinalSize.height); 
     weakImage.center = CGPointMake(weakView.frame.size.width/2, weakView.frame.size.height/2); 
    } 
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) 
    { 
     NSLog(@"%@",error); 
    }]; 
} 

// Set new title 
NSInteger marginLeft = self.frame.size.width/20; 
NSInteger marginBottom = self.frame.size.height/20; 

titleLabel.text = module.title; 
titleLabel.font = [self fontWithModuleSize]; 

CGSize newSize = [module.title sizeWithFont:titleLabel.font constrainedToSize:titleLabel.frame.size]; 
CGRect newRect = CGRectMake(10+marginLeft, 
          self.frame.size.height - newSize.height - marginBottom, 
          newSize.width-30, 
          newSize.height); 
titleLabel.frame = newRect; 
} 
相關問題