[英]Using a UIImageView to adjust a UIImage
我有一個UIImage,並且我想基於UIImageView調整設備上顯示的內容。
UIImage是:
UIImage *image = // a PNG I have
// width = 1200
CGFloat width = image.size.width;
// height = 900
CGFloat height = image.size.height;
我有一個UIImageView
UIImageView *iview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 675.0, 900.0)];
如何在不調整縱橫比的情況下將UIImage放入UIImageView? 我想將UIImage裁剪掉嗎?
希望這可以幫助
-(UIImage*)crop:(CGRect)frame
{
// Find the scalefactors UIImageView's widht and height / UIImage width and height
CGFloat widthScale = self.bounds.size.width / self.image.size.width;
CGFloat heightScale = self.bounds.size.height / self.image.size.height;
// Calculate the right crop rectangle
frame.origin.x = frame.origin.x * (1 / widthScale);
frame.origin.y = frame.origin.y * (1 / heightScale);
frame.size.width = frame.size.width * (1 / widthScale);
frame.size.height = frame.size.height * (1 / heightScale);
// Create a new UIImage
CGImageRef imageRef = CGImageCreateWithImageInRect(self.image.CGImage, frame);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;//(return value is uiimage)
}
然后將其添加到uiimageview
嘗試將UIImageView的contentMode
屬性設置為UIViewContentModeScaleAspectFill
,根據文檔,該屬性是:
The option to scale the content to fill the size of the view. Some portion of the
content may be clipped to fill the view’s bounds.
您可以在UIView文檔中看到其他選項。
如果您想修剪邊緣並居中圖像的位置,則將Imageview屬性設置為AspectFill
myImageView.contentMode = UIViewContentModeScaleAspectFill;
myImageView.clipsToBounds = YES;
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.