gpt4 book ai didi

ios - 如何绘制UIImage图像如装修风格

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:15:59 28 4
gpt4 key购买 nike

我想从 jpg 文件中获取缩略图并显示为 UIImage。显示样式为适配模式,如UIViewContentModeScaleAspectFill。下面是示例代码,但是太复杂了。

-(UIImage*)scaleToSize:(CGSize)size   
{
CGFloat width = CGImageGetWidth(self.CGImage);
CGFloat height = CGImageGetHeight(self.CGImage);

NSLog(@"size w=%f, h=%f, image w=%f, h=%f", size.width, size.height, width, height);

float verticalRadio = size.height*1.0/height;
float horizontalRadio = size.width*1.0/width;

float radio = 1;
if(verticalRadio>1 && horizontalRadio>1)
{
radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;
}
else
{
radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;
}

width = width*radio;
height = height*radio;
NSLog(@"width=%f, height=%f", width, height);

int xPos = (size.width - width)/2;
int yPos = (size.height-height)/2;

NSLog(@"xpos=%d, ypos=%d", xPos, yPos);

CGSize sz = CGSizeMake(width, height);
UIGraphicsBeginImageContext(sz);

//
[self drawInRect:CGRectMake(0, 0, width, height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGRect rt = CGRectMake(-xPos, -yPos, size.width, size.height);
UIImage* thub = [scaledImage getSubImage:rt];

scaledImage = nil;
return thub;
}

- (UIImage *)getSubImage:(CGRect) rect{
CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
CGRect smallBounds = CGRectMake(rect.origin.x, rect.origin.y, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));

NSLog(@"small bounds x=%f, y=%f, w=%f, h=%f", smallBounds.origin.x, smallBounds.origin.y, smallBounds.size.width, smallBounds.size.height);

UIGraphicsBeginImageContext(smallBounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, smallBounds, subImageRef);
UIImage* smallImg = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();

return smallImg;
}

最佳答案

我已经更新了方法:

-(UIImage*)getSubImage:(CGSize)size   
{
CGFloat width = CGImageGetWidth(self.CGImage);
CGFloat height = CGImageGetHeight(self.CGImage);

float verticalRadio = size.height*1.0/height;
float horizontalRadio = size.width*1.0/width;

float radio = 1;
radio = verticalRadio < horizontalRadio ? horizontalRadio : verticalRadio;

CGRect displayRect = CGRectMake((size.width - width*radio)/2.0f,
(size.height-height*radio)/2.0f,
width*radio,
height*radio);


// create a bitmap context and then set the context to current using
UIGraphicsBeginImageContext(size);

//clip
UIRectClip(displayRect);

//draw the bitmap in rect
[self drawInRect:displayRect];

// from context to get a UIIMage
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

// move context out of buffer
UIGraphicsEndImageContext();

NSLog(@"getSubImage ---->");

// return.
return scaledImage;
}

关于ios - 如何绘制UIImage图像如装修风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185529/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com