gpt4 book ai didi

ios - 保存 UIImage 的缩略图会横向/上下翻转图像

转载 作者:行者123 更新时间:2023-11-29 01:51:00 25 4
gpt4 key购买 nike

我正在保存 UIImage 的缩略图以提高我的 UICollectionView 滚动的性能。在我尝试保存自下而上拍摄的全景照片之前,所有图像都是直立保存的(例如:高度 - 3000,宽度 - 1000)。这倒存了。我拍了一张人像照片并旋转了 90 度,因为最初图像是横向的。有没有办法用一行代码将每种类型的图像设置为直立,或者我是否必须捕捉每种类型的照片并相应地进行调整?如果是这样怎么办?

 - (void) createThumbnail: (UIImage *) image{
CGSize size = image.size;
CGFloat imageHeight = size.height;
CGFloat imageWidth = size.width;

CGSize croppedSize;
CGFloat ratio = 200.0;
CGFloat offsetX = 0.0;
CGFloat offsetY = 0.0;

if (imageWidth > imageHeight) {
offsetX = (imageHeight - imageWidth) / 2;
croppedSize = CGSizeMake(imageHeight, imageHeight);
} else {
offsetY = (imageWidth - imageHeight) / 2;
croppedSize = CGSizeMake(imageWidth, imageWidth);
}


CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);

CGRect rect = CGRectMake(0.0, 0.0, ratio, ratio);

UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef] drawInRect:rect];
UIImage *thumbnail;



if ((imageWidth > imageHeight) || (imageWidth == imageHeight)) {
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationUp];
}else{
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationRight];
}

UIGraphicsEndImageContext();
return thumbnail;

最佳答案

我必须从 didFinishPickingMediaWithInfo 中选择的图像而不是裁剪的缩略图获取图像方向。

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
choosenImage = info[UIImagePickerControllerOriginalImage];
long orientation = choosenImage.imageOrientation;

UIImage *thumbnail = [self createThumbnail:choosenImage withOrientation: orientation];
}

 UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef] drawInRect:rect];
UIImage *thumbnail;

if (orientation == UIImageOrientationUp) {
thumbnail = UIGraphicsGetImageFromCurrentImageContext();
}else if (orientation == UIImageOrientationRight){
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationRight];
}else if (orientation == UIImageOrientationLeft){
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationLeft];
}else if (orientation == UIImageOrientationDown){
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationUpMirrored];
}

UIGraphicsEndImageContext();

关于ios - 保存 UIImage 的缩略图会横向/上下翻转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31420356/

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