2014-01-13 122 views
0

我有以下代碼:圖像沒有填充整個的UIImageView寬度和高度

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呈現的圖像時是沒有填充整個幀.size並總是「短」

可以點一下在我的代碼中修復/添加什麼?

回答

1

設置圖像視圖的內容模式填補reference):

self.captureImageView.contentMode = UIViewContentModeScaleToFill; 
+0

將其添加到代碼,它不工作:( –

+0

@liva你是否設置它時,視圖控制器第一次加載?它應該只需要設置一次 – trojanfoe

+0

是把它放在viewDidLoad –

0

假設你正在使用自動佈局要確保圖像被固定在上海華兩側(無論是在IB還是像我在下面做的代碼)。

您還需要像提及的@trojanfoe(在IB或代碼中)那樣設置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]; 

注:selfimageView此代碼段中的上海華。