[英]The image is not filling the entire uiimageview width and height
我有以下代碼:
UIGraphicsBeginImageContext(CGSizeMake(self.captureImageView.frame.size.width, self.captureImageView.frame.size.height));
[image drawInRect: CGRectMake(0, 0, self.captureImageView.frame.size.width, self.captureImageView.frame.size.height)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGRect cropRect = CGRectMake(0, 0, self.captureImageView.frame.size.width, self.captureImageView.frame.size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([smallImage CGImage], cropRect);
[self.captureImageView setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);
//rotation
[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:0.5];
int degrees = [self rotationDegrees];
CGFloat radians =degrees * M_PI / 180.0;
self.captureImageView.transform = CGAffineTransformMakeRotation(radians);
[UIView commitAnimations];
在橫向模式下捕獲圖像時,向左或向右顯示的uiimageview中的圖像沒有填充整個frame.size,並且始終為“短”
可以指出我的代碼要修復/添加的內容嗎?
將圖像視圖的內容模式設置為填充 ( 參考 ):
self.captureImageView.contentMode = UIViewContentModeScaleToFill;
假設您正在使用自動布局,則要確保將圖像固定在超級視圖的側面(在IB或代碼中,如下所示)。
您還需要像在IB或代碼中提到的@trojanfoe一樣設置contentMode
。 我正在使用UIViewContentModeScaleAspectFit
(而不是UIViewContentModeScaleToFill
)來保留圖像的長寬比。
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:imageView];
NSArray *horzConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[imageView]-(0)-|"
options: NSLayoutFormatAlignAllCenterX
metrics:nil
views:@{@"imageView" : imageView}];
NSArray *vertConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[imageView]-(0)-|"
options: NSLayoutFormatAlignAllCenterY
metrics:nil
views:@{@"imageView" : imageView}];
[self addConstraints:horzConstraints];
[self addConstraints:vertConstraints];
注意: self
是此代碼段中imageView的imageView
視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.