gpt4 book ai didi

iPhone 横向 View 屏幕截图

转载 作者:行者123 更新时间:2023-12-03 19:16:19 28 4
gpt4 key购买 nike

我正在横向模式下运行应用程序。我需要注意什么才能使拍摄的屏幕截图也处于横向模式。

我在 iPhone 应用程序中使用以下代码,底部有标签栏并拍摄屏幕截图,但它始终仅处于纵向模式。为什么?

UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];

UIGraphicsBeginImageContext(screenWindow.frame.size);
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

最佳答案

我之前的回答是错误的。我已经研究过这个问题,并且我有一个横向右屏幕截图的答案。这会将横向右侧屏幕截图正确保存到相机胶卷中。我还没有尝试过横向左。

这是另一个 stackoverflow 问题 here 中提供的答案的变体。该答案的问题在于屏幕截图始终具有 UIImageOrientationUp 的 imageOrientation(只读)。因此,我们需要知道方向是什么并适本地改变它。

我希望这有帮助。如果这适用于横向左侧,或者您获得适用于横向左侧的变体,请发布它。

 - (IBAction)cameraButtonClick:(id)sender {

UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];

UIGraphicsBeginImageContext(screenWindow.frame.size);
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();


UIImageWriteToSavedPhotosAlbum([self scaleAndRotateImage:screenshot], nil, nil, nil);

UIGraphicsEndImageContext();
}

// Code from: http://discussions.apple.com/thread.jspa?messageID=7949889
- (UIImage *)scaleAndRotateImage:(UIImage *)image {
int kMaxResolution = 640; // Or whatever

CGImageRef imgRef = image.CGImage;

CGFloat width = CGImageGetWidth(imgRef);
CGFloat height = CGImageGetHeight(imgRef);


CGAffineTransform transform = CGAffineTransformIdentity;
CGRect bounds = CGRectMake(0, 0, width, height);
if (width > kMaxResolution || height > kMaxResolution) {
CGFloat ratio = width/height;
if (ratio > 1) {
bounds.size.width = kMaxResolution;
bounds.size.height = roundf(bounds.size.width / ratio);
}
else {
bounds.size.height = kMaxResolution;
bounds.size.width = roundf(bounds.size.height * ratio);
}
}

CGFloat scaleRatio = bounds.size.width / width;
CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
CGFloat boundHeight;

boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width/2.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);

UIGraphicsBeginImageContext(bounds.size);

CGContextRef context = UIGraphicsGetCurrentContext();

UIImageOrientation orient = image.imageOrientation;

if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, -height, 0);
}
else {
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
}

CGContextConcatCTM(context, transform);

CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return imageCopy;
}

关于iPhone 横向 View 屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2857403/

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