前言: 淺顯易讀, 看完就能明白的小技巧, 兩三分鐘足夠了, 非常實用, 點到為止.
把一個 64 x 64 的圖片 賦值給 size = 200 x 200的ImageView
UIImageView *imageView = [UIImageView new]; imageView.backgroundColor = [UIColor cyanColor]; imageView.frame = CGRectMake(0, 0, 200, 200); UIImage *image = [UIImage imageNamed:@"rect"]; imageView.image = image; imageView.center = CGPointMake(self.view.frame.size.width / 2, 200); [self.view addSubview:imageView];
很明顯模糊的非常嚴重, 特別的圓角邊
在iOS5 之後 系統提供一個API: 對邊緣部分不進行拉伸處理
image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(25, 25, 25, 25)];
iOS6 提供的API
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode
處理方式多了個參數可選類型
typedef NS_ENUM(NSInteger, UIImageResizingMode) { UIImageResizingModeTile, // tile瓷磚的意思: 一塊一塊把牆面貼滿 UIImageResizingModeStretch, // 單純拉伸 };