2015-05-29 20 views
10

Tôi muốn thay đổi kích thước hình ảnh bằng phương thức drawInRect, nhưng tôi cũng muốn duy trì tỷ lệ khung hình phù hợp, trong khi điền đầy đủ khung hình đã cho (như .ScaleAspectFill làm cho UIViewContentMode). Bất kỳ ai cũng có câu trả lời cho câu hỏi này?Thay đổi kích thước hình ảnh bằng drawInRect trong khi vẫn duy trì tỷ lệ khung hình như Scale Aspect Fill?

Đây là mã của tôi (khá đơn giản ...):

func scaled100Image() -> UIImage { 
    let newSize = CGSize(width: 100, height: 100) 
    UIGraphicsBeginImageContext(newSize) 
    self.pictures[0].drawInRect(CGRect(x: 0, y: 0, width: 100, height: 100)) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return newImage 
} 

Trả lời

24

OK, vì vậy không trả lời làm sẵn ... Tôi đã viết một phần mở rộng nhanh chóng cho UIImage, cảm thấy tự do để sử dụng nó nếu bạn cần nó.

Ở đây là:

extension UIImage { 
    func drawInRectAspectFill(rect: CGRect) { 
     let targetSize = rect.size 
     if targetSize == CGSizeZero { 
      return self.drawInRect(rect) 
     } 
     let widthRatio = targetSize.width/self.size.width 
     let heightRatio = targetSize.height/self.size.height 
     let scalingFactor = max(widthRatio, heightRatio) 
     let newSize = CGSize(width: self.size.width * scalingFactor, 
          height: self.size.height * scalingFactor) 
     UIGraphicsBeginImageContext(targetSize) 
     let origin = CGPoint(x: (targetSize.width - newSize.width)/2, 
          y: (targetSize.height - newSize.height)/2) 
     self.drawInRect(CGRect(origin: origin, size: newSize)) 
     let scaledImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     scaledImage.drawInRect(rect) 
    } 
} 

Vì vậy, trong ví dụ trên, bạn sử dụng nó như thế:

self.pictures[0].drawInRectAspectFill(CGRect(x: 0, y: 0, width: 100, height: 100)) 
+0

Bạn là anh hùng của tôi – MScottWaller

+0

Không tốt cho tôi. Tôi mất 40MB mỗi khi tôi chạy nó và nó làm hỏng ứng dụng của tôi bởi vì tôi đang cố gắng chia nhỏ nhiều ảnh cùng một lúc. – RowanPD

+0

Sử dụng tỷ lệ để hình ảnh không bị mờ - 'UIGraphicsBeginImageContextWithOptions (targetSize, true, UIScreen.mainScreen(). Scale)' –

1

Phiên bản Objective-C, nếu ai đó cần nó (Dán mã này bên trong một UIImage loại):

- (void) drawInRectAspectFill:(CGRect) recto { 

CGSize targetSize = recto.size; 
if (targetSize.width <= CGSizeZero.width && targetSize.height <= CGSizeZero.height) { 
    return [self drawInRect:recto]; 
} 

float widthRatio = targetSize.width/self.size.width; 
float heightRatio = targetSize.height/self.size.height; 
float scalingFactor = fmax(widthRatio, heightRatio); 
CGSize newSize = CGSizeMake(self.size.width * scalingFactor, self.size.height * scalingFactor); 

UIGraphicsBeginImageContext(targetSize); 

CGPoint origin = CGPointMake((targetSize.width-newSize.width)/2,(targetSize.height - newSize.height)/2); 

[self drawInRect:CGRectMake(origin.x, origin.y, newSize.width, newSize.height)]; 
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

[scaledImage drawInRect:recto]; 

}

Các vấn đề liên quan