1

我有以下代码:

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 并且总是“短”

有人可以在我的代码中修复/添加什么吗?

4

2 回答 2

1

将图像视图的内容模式设置为填充参考):

self.captureImageView.contentMode = UIViewContentModeScaleToFill;
于 2014-01-13T12:40:47.407 回答
0

假设您正在使用自动布局,您希望确保图像固定在超级视图的两侧(在 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];

注意:selfimageView此代码片段中的超级视图。

于 2015-07-27T23:52:40.207 回答