2015-10-29 20 views
0

嗨,就像我在標題上所說的那樣,我試圖在與UIImage的最大尺寸相匹配的正方形內繪製完整的UIImage。我的問題是我如何讓我的代碼工作。我的代碼正在裁剪的是裁剪到正方形大小。我是新來的IO可以任何一個幫助。如何在邊緣上用白色正方形繪製完整UIImage

PS至少需要調用CGcontext的方法。

謝謝。

- (UIImage*)imageWithBorderFromImage:(UIImage*)source; 
{ 
    CGSize size = [source size]; 
    CGFloat width = size.width; 
    CGFloat height = size.height; 
    CGSize resizedImageSize; 
    if (width < height) { 
     resizedImageSize = CGSizeMake(height, height); 
    }else { 
     resizedImageSize = CGSizeMake(width, width); 
    } 
    UIGraphicsBeginImageContext(resizedImageSize); 
    CGRect rect = CGRectMake(0, 0, resizedImageSize.width, resizedImageSize.height); 
    [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 1.0, 0.5, 1.0, 1.0); 
    CGContextStrokeRect(context, rect); 
    UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return testImg; 
} 

我在找的結果類型。

enter image description here

+0

使用寬高比來計算新寬度和高度。 –

+0

我已經做了? 'if(width skip87

+1

最簡單的方法就是使用'UIImageView'。調整'contentMode'並將邊框添加到'UIImageView'。您可以通過'UIView'的'layer'屬性添加'border'並設置'border color'。該層是類CALayer。 – AechoLiu

回答

2

看起來你比你認識到實現自己的目標越來越近。調用drawInRect時,需要調整使用的矩形:保持寬高比並適合新圖像矩形的中間。請記住,那裏使用的矩形不會規定新圖像的大小,傳遞給UIGraphicsBeginImageContext的值會定義該值。

- (UIImage *)squareImageFromImage:(UIImage *)image 
{ 
    // Get a square that the image will fit into 
    CGFloat maxSize = MAX(image.size.height, image.size.width); 
    CGSize squareSize = CGSizeMake(maxSize, maxSize); 

    // Get our offset to center the image inside our new square frame 
    CGFloat dx = (maxSize - image.size.width)/2.0f; 
    CGFloat dy = (maxSize - image.size.height)/2.0f; 

    UIGraphicsBeginImageContext(squareSize); 
    CGRect rect = CGRectMake(0, 0, maxSize, maxSize); 

    // Draw a white background and set a magenta stroke around the entire square image (debugging only?) 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); 
    CGContextSetStrokeColorWithColor(context, [UIColor magentaColor].CGColor); 
    CGContextFillRect(context, rect); 
    CGContextStrokeRect(context, rect); 

    // Adjust the rect to be centered in our new image 
    rect = CGRectInset(rect, dx, dy); 
    [image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; 

    UIImage *squareImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return squareImage; 
} 

這需要此樣本圖像 Before Image, Rectangular

並給你這個新的正方形圖像 Processed Image, Square

+0

非常感謝 – skip87

1

在夫特3

extension UIImage { 

    func squareMe() -> UIImage { 

     var squareImage = self 
     let maxSize = max(self.size.height, self.size.width) 
     let squareSize = CGSize(width: maxSize, height: maxSize) 

     let dx = CGFloat((maxSize - self.size.width)/2) 
     let dy = CGFloat((maxSize - self.size.height)/2) 

     UIGraphicsBeginImageContext(squareSize) 
     var rect = CGRect(x: 0, y: 0, width: maxSize, height: maxSize) 

     if let context = UIGraphicsGetCurrentContext() { 
      context.setFillColor(UIColor.clear.cgColor) 
      context.fill(rect) 

      rect = rect.insetBy(dx: dx, dy: dy) 
      self.draw(in: rect, blendMode: .normal, alpha: 1.0) 

      if let img = UIGraphicsGetImageFromCurrentImageContext() { 
       squareImage = img 
      } 
      UIGraphicsEndImageContext() 

     } 

     return squareImage 
    } 

}