iOS 圖片圓角處理

圓角的處理

來自 <code> AsyncDisplayKit </code>的一個Deomo <code>SocialAppLayout</code> 這是一個 類似新浪微博,app的布局單頁面。其中頭像是圓角的,我們都知道,如果,使用layer來處理圓角,性能肯定有損耗。接下來,我們看看 SocialAppLayout 怎么做的。

有興趣的可以從git上下載代碼看看。

// User pic 頭像Node 如果不懂 node 可以翻看我之前的博客,不過沒所謂,可以忽略,我們重點看圓角。
        _avatarNode = [[ASNetworkImageNode alloc] init];
        ///此處為設置圓角
        _avatarNode.imageModificationBlock = ^UIImage *(UIImage *image) {
            
            UIImage *modifiedImage;
            CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
            
            UIGraphicsBeginImageContextWithOptions(image.size, false, [[UIScreen mainScreen] scale]);
            
            [[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:44.0] addClip];
            [image drawInRect:rect];
            modifiedImage = UIGraphicsGetImageFromCurrentImageContext();
            
            UIGraphicsEndImageContext();
            
            return modifiedImage;
            
        };

imageModificationBlock 會在圖片解碼完成之后調用,此處,參數 image 是解碼后的正方形圖片。

通過

[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:44.0] addClip];

設置圓角。(PS:項目中可以直接使用)。

當然不是每次都做圓角的處理,緩存還是要做的。

……
///獲取 ASWeakMapEntry 對象
ASWeakMapEntry<UIImage *> *entry = [self.class contentsForkey:contentsKey isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled];
    if (entry == nil) {  // If nil, we were cancelled.
        return nil;
    }
    _weakCacheEntry = entry; // Retain so that the entry remains in the weak cache
    
……

<code>ASWeakMapEntry</code>的聲明如下:

@interface ASWeakMapEntry<Value> : NSObject

@property (nonatomic, retain, readonly) Value value;

@end

此處就僅僅存放了一個 UIImage 對象。

下面看從緩存獲取圖片的過程

+ (ASWeakMapEntry *)contentsForkey:(ASImageNodeContentsKey *)key isCancelled:(asdisplaynode_iscancelled_block_t)isCancelled
{
  {
    ASDN::MutexLocker l(cacheLock);
    if (!cache) {
      cache = [[ASWeakMap alloc] init];
    }
    ///如果緩存中有,直接返回
    ASWeakMapEntry *entry = [cache entryForKey:key];
    if (entry != nil) {
      // cache hit
      return entry;
    }
  }

  // 緩存中沒有,直接創建。
  UIImage *contents = [self createContentsForkey:key isCancelled:isCancelled];
  if (contents == nil) { // If nil, we were cancelled
    return nil;
  }

  {
    ASDN::MutexLocker l(cacheLock);
    return [cache setObject:contents forKey:key];
  }
}

Entry 對象都存在 ASWeakMap 中。

此處緩存中沒有的話,直接調用 createContentsForkey 方法去創建。創建過程,無非就是解碼當前圖片,解碼完了調用 imageModificationBlock 。這里之所以使用block 因為,說不定有其他效果什么的。

<code>ASWeakMap</code>的聲明如下:

@interface ASWeakMap<__covariant Key : NSObject *, Value> : NSObject

/**
 * Read from the cache.  The Value object is accessible from the returned ASWeakMapEntry.
 */
- (nullable ASWeakMapEntry<Value> *)entryForKey:(Key)key;

/**
 * Put a value into the cache.  If an entry with an equal key already exists, then the value is updated on the existing entry.
 */
- (ASWeakMapEntry<Value> *)setObject:(Value)value forKey:(Key)key;

@end

如果有圖片圓角影響性能的,可以參考此處做法。ASWeakMap 可以直接從 AsyncDisplayKit 中扒出來使用的,沒有任何依賴。。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容