2013-04-14 69 views
0

不使用對的UIImageView AFNetworking的分類方法時,此工作,但我意識到AF可能是覆蓋不管,因爲這樣做是發生異步:設置UIImageView的圓潤與AFNetworking setImageWithURL角落:

這是我自定義的UITableViewCell(自己是單元格):

NSURL *url = [NSURL URLWithString:video.thumbnail];  
[self.imageView setImageWithURL:url 
       placeholderImage:[UIImage imageNamed:@"Logo.png"]]; 
// not working 
CALayer *layer = [self.imageView layer]; 
[layer setMasksToBounds:YES]; 
[layer setCornerRadius:10.0]; 

嘗試將它放在調用setImageWithURL之前。

這是我現在並沒有四捨五入的角落:

NSURL *url = [NSURL URLWithString:_video.thumbnail]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
UIImage *placeHolder = [UIImage imageNamed:@"Logo"]; 
__weak UIImageView *imageView = self.thumbnail; 

[self.thumbnail setImageWithURLRequest:request placeholderImage:placeHolder success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 

    imageView.image = image; 
    // round the edges of the video thumbnail 
    CALayer *layer = [imageView layer]; 
    [layer setMasksToBounds:YES]; 
    [layer setCornerRadius:8.0]; 

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
    NSLog(@"couldn't load image: %@", [error localizedDescription]); 
}]; 
+0

當你從一個url下載圖片時,需要一些時間取決於互聯網的速度,在這種情況下,imageView沒有正確加載來自url的圖片。您應該使用阻止或異步來首先完全下載圖像,然後從中提取圖層。 – MrWaqasAhmed

回答

2

我從來沒有用過AFNetworking但我看到有是setImageWithURL方法的版本,需要一個「成功」塊。嘗試使用該版本並在「成功」塊中更新圖像視圖的圖層。

5
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
__weak UIImageView *imageView = self.imageView; 
[self.imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"Logo.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
    imageView.image = image; 
    CALayer *layer = imageView.layer; 
    layer.masksToBounds = YES; 
    layer.cornerRadius = 10.0f; 
} failure:NULL]; 
+0

這就是我在回答中所談論的那個。 :) – rmaddy

+0

由於某些原因無法正常工作,請確認上面的確切代碼。 –

+0

@MarkW你是否'#import ' –

0

的SDWebImage庫,可在https://github.com/rs/SDWebImage,包括方法:

  • (無效)setImageWithURL:(NSURL *)URL placeholderImage:(*的UIImage)佔位符完成:(SDWebImageCompletedBlock)completedBlock;

以下代碼是我正在處理的項目的一個示例實現,它有一個相當大的圓角。我在同一個項目中使用了AFNetworking和SDWebImage。

__weak UIImageView * theImageView = self.imageView; 

    [self.imageView setImageWithURL:[NSURL URLWithString:user.imgurl] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) { 
     // I only attempt to apply this if an image has been returned. 
     if (image) { 
      theImageView.image = image; 
      CALayer * layer = theImageView.layer; 
      layer.masksToBounds = YES; 
      layer.cornerRadius = 20.0f; 
     } 

    }];