![](/img/trans.png)
[英]How to get pixel color at location from UIimage scaled within a UIimageView
[英]How to get the size of a scaled UIImage in UIImageView?
UIImageView
的image.size
屬性給出了原始UIImage
的大小。 我想知道自動縮放圖像放入UIImageView
時的大小(通常小於原始圖像)。
例如,我將圖像設置為Aspect Fit
。 現在我想知道它在屏幕上的新高度和寬度,以便我可以准確地在新的縮放圖像上繪制。
有沒有辦法做到這一點,而不是根據UIImageView大小和UIImage原始大小自己搞清楚(基本上是逆向工程縮放)?
Objective-C的:
-(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView
{
float imageRatio = image.size.width / image.size.height;
float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
if(imageRatio < viewRatio)
{
float scale = imageView.frame.size.height / image.size.height;
float width = scale * image.size.width;
float topLeftX = (imageView.frame.size.width - width) * 0.5;
return CGRectMake(topLeftX, 0, width, imageView.frame.size.height);
}
else
{
float scale = imageView.frame.size.width / image.size.width;
float height = scale * image.size.height;
float topLeftY = (imageView.frame.size.height - height) * 0.5;
return CGRectMake(0, topLeftY, imageView.frame.size.width, height);
}
}
斯威夫特4:
func frame(for image: UIImage, inImageViewAspectFit imageView: UIImageView) -> CGRect {
let imageRatio = (image.size.width / image.size.height)
let viewRatio = imageView.frame.size.width / imageView.frame.size.height
if imageRatio < viewRatio {
let scale = imageView.frame.size.height / image.size.height
let width = scale * image.size.width
let topLeftX = (imageView.frame.size.width - width) * 0.5
return CGRect(x: topLeftX, y: 0, width: width, height: imageView.frame.size.height)
} else {
let scale = imageView.frame.size.width / image.size.width
let height = scale * image.size.height
let topLeftY = (imageView.frame.size.height - height) * 0.5
return CGRect(x: 0.0, y: topLeftY, width: imageView.frame.size.width, height: height)
}
}
這個簡單的函數將計算圖像的大小: -
[imageView setFrame:AVMakeRectWithAspectRatioInsideRect(image.size, imageView.frame)];
有關更多詳細信息,您可以參考AVMakeRectWithAspectRatioInsideRect - AVFoundation 。
這個簡單的函數將在方面擬合后計算圖像的大小:
-(CGSize)imageSizeAfterAspectFit:(UIImageView*)imgview{
float newwidth;
float newheight;
UIImage *image=imgview.image;
if (image.size.height>=image.size.width){
newheight=imgview.frame.size.height;
newwidth=(image.size.width/image.size.height)*newheight;
if(newwidth>imgview.frame.size.width){
float diff=imgview.frame.size.width-newwidth;
newheight=newheight+diff/newheight*newheight;
newwidth=imgview.frame.size.width;
}
}
else{
newwidth=imgview.frame.size.width;
newheight=(image.size.height/image.size.width)*newwidth;
if(newheight>imgview.frame.size.height){
float diff=imgview.frame.size.height-newheight;
newwidth=newwidth+diff/newwidth*newwidth;
newheight=imgview.frame.size.height;
}
}
NSLog(@"image after aspect fit: width=%f height=%f",newwidth,newheight);
//adapt UIImageView size to image size
//imgview.frame=CGRectMake(imgview.frame.origin.x+(imgview.frame.size.width-newwidth)/2,imgview.frame.origin.y+(imgview.frame.size.height-newheight)/2,newwidth,newheight);
return CGSizeMake(newwidth, newheight);
}
cncool答案的改編,在swift中作為UIImageView擴展,也許它可能對某人有用:
extension UIImageView{
func frameForImageInImageViewAspectFit() -> CGRect
{
if let img = self.image {
let imageRatio = img.size.width / img.size.height;
let viewRatio = self.frame.size.width / self.frame.size.height;
if(imageRatio < viewRatio)
{
let scale = self.frame.size.height / img.size.height;
let width = scale * img.size.width;
let topLeftX = (self.frame.size.width - width) * 0.5;
return CGRectMake(topLeftX, 0, width, self.frame.size.height);
}
else
{
let scale = self.frame.size.width / img.size.width;
let height = scale * img.size.height;
let topLeftY = (self.frame.size.height - height) * 0.5;
return CGRectMake(0, topLeftY, self.frame.size.width, height);
}
}
return CGRectMake(0, 0, 0, 0)
}
}
由於您已將圖像視圖設置為Aspect Fit,因此您可以簡單地獲取圖像視圖的高度和寬度以及原始圖像的高度和寬度,並計算縮放圖像的高度和寬度。
也就是說, 更好的方法是使用自定義視圖而不是UIImageView。 然后,在自定義視圖中自己繪制圖像。 這樣你就可以控制它的每個方面。
關注@MeetDoshi,這是一個應該適應這種情況的擴展。
extension UIImageView {
/// Retrieve the scaled size of the image within this ImageView.
/// - Returns: A CGRect representing the size of the image after scaling or nil if no image is set.
func getScaledImageSize() -> CGRect? {
if let image = self.image {
return AVMakeRect(aspectRatio: image.size, insideRect: self.frame);
}
return nil;
}
}
我寫了一個有用的擴展,其中包含了它的一組添加內容,可能值得一試。
包含的功能
func getScaledImageSize() -> CGRect?
func getScaledImageSizeWithPadding() -> CGRect?
func getPaddingTop() -> CGFloat? {
func getPaddingBottom() -> CGFloat? {
func getPaddingLeft() -> CGFloat? {
func getPaddingRight() -> CGFloat?
func getScaledCoordinate(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint?
func getScaledCoordinateWithPadding(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint?
https://gist.github.com/nathan-fiscaletti/e1b85f68559f305db7342bfff9574563
如果在保持圖像寬高比的情況下填充幀,則圖像視圖幀將與圖像大小不同。
如何從框架中獲取UIImageView大小? 即imageView.frame?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.