2014-10-16 15 views
9

enter image description hereChế độ nội dung UIImageView

Đường màu xanh lam là giới hạn của số lần xem trang. UIImageView 's contentModeUIViewContentModeScaleAspectFit, Và tôi muốn giữ tỷ lệ hình ảnh ban đầu. Làm cách nào để làm cho mép trái của hình ảnh nằm ở cạnh trái của UIImageView? Nhưng không giống như chế độ UIViewContentModeTopLeft. Giữ tỷ lệ và không để trống ở bên trái. Tiếng Anh của tôi không tốt, hy vọng các bạn có thể hiểu những gì tôi đang nói. Cảm ơn bạn rất nhiều.

+0

đặt chế độ nội dung của bạn Là UIViewContentModeScaleAspectFill .. –

Trả lời

6
yourimageView.contentMode = UIViewContentModeCenter; 
if (yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height) { 
    yourimageView.contentMode = UIViewContentModeScaleAspectFit; 
} 

Swift3

yourimageView.contentMode = .center 
if yourimageView.bounds.size.width > yourimageView.size.width && yourimageView.bounds.size.height > yourimageView.size.height { 
yourimageView.contentMode = .scaleAspectFit 
} 

bạn có thể thay đổi chế độ của bạn như

typedef NS_ENUM(NSInteger, UIViewContentMode) { 
UIViewContentModeScaleToFill, 
UIViewContentModeScaleAspectFit,  // contents scaled to fit with fixed aspect. remainder is transparent 
UIViewContentModeScaleAspectFill,  // contents scaled to fill with fixed aspect. some portion of content may be clipped. 
UIViewContentModeRedraw,    // redraw on bounds change (calls -setNeedsDisplay) 
UIViewContentModeCenter,    // contents remain same size. positioned adjusted. 
UIViewContentModeTop, 
UIViewContentModeBottom, 
UIViewContentModeLeft, 
UIViewContentModeRight, 
UIViewContentModeTopLeft, 
UIViewContentModeTopRight, 
UIViewContentModeBottomLeft, 
UIViewContentModeBottomRight, 
}; 
0

chỉ như thế này

imageView.contentMode = UIViewContentModeCenter; 
if (imageView.bounds.size.width > imageView.size.width && imageView.bounds.size.height > imageView.size.height) { 
    imageView.contentMode = UIViewContentModeScaleAspectFit; 
} 
0

thời điểm tôi thấy tình hình của bạn, tôi nghĩ về vấn đề này:

_imageView.contentMode = UIViewContentModeLeft | UIViewContentModeScaleAspectFit; 

và tôi đã cố gắng nó, nhưng nó làm cho điều tồi tệ hơn

vì vậy tôi nghĩ rằng bạn có thể thay đổi kích thước imageView`s của bạn chắc chắn sẽ phù hợp với kích thước bạn muốn với mã này

/// resize image to fit in show area without changing image ratio 
+ (CGSize)resizeImageViewFromImageSize:(CGSize)imageSize toFitShowSize:(CGSize)showSize { 
CGFloat ratioW = imageSize.width/showSize.width; 
CGFloat ratioH = imageSize.height/showSize.height; 

CGFloat ratio = imageSize.width/imageSize.height; 

if (ratioW > 1 && ratioH > 1) { 

    if (ratioW > ratioH) { 
     imageSize.width = showSize.width; 
     imageSize.height = imageSize.width/ratio; 
    } else { 
     imageSize.height = showSize.height; 
     imageSize.width = imageSize.height * ratio; 
    } 

} else if (ratioW > 1) { 

    imageSize.width = showSize.width; 
    imageSize.height = imageSize.width/ratio; 

} else if (ratioH > 1) { 

    imageSize.height = showSize.height; 
    imageSize.width = imageSize.height * ratio; 

} else { 

    if (ratioW > ratioH) { 

     if (showSize.width/imageSize.width <= 2) { 
      imageSize.width = showSize.width; 
      imageSize.height = imageSize.width/ratio; 
     } 

    } else { 

     if (showSize.height/imageSize.height <= 2) { 
      imageSize.height = showSize.height; 
      imageSize.width = imageSize.height * ratio; 
     } 

    } 

} 

return imageSize; 
} 

sau đó đặt class IMAGExem của bạn sang trái của màn hình

0

Swift

import UIKit 
import AVFoundation 



let originalsize = CGSizeMake(500, 500) 
let rect = CGRectMake(0, 0, 320, 480) 


var result = AVMakeRectWithAspectRatioInsideRect(originalsize, rect) 

print(result) 
Các vấn đề liên quan