图像未填充整个uiimageview的宽度和高度

时间:2014-01-13 12:37:31

标签: objective-c uiimageview uideviceorientation

我有以下代码:

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并始终“短”

可以指出我的代码中要修复/添加的内容吗?

2 个答案:

答案 0 :(得分:1)

将图片视图的内容模式设置为 fill reference):

self.captureImageView.contentMode = UIViewContentModeScaleToFill;

答案 1 :(得分:0)

假设您正在使用 autolayout ,您希望确保将图像固定到超级视图的两侧(在IB中或在代码中,如下所示)。

您还需要设置contentMode之类的@trojanfoe(在IB或代码中)。我使用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的超级视图。