gpt4 book ai didi

ios - 使用 UIGraphicsBeginImageContext 将 UICollectionViewCell 保存为图像

转载 作者:行者123 更新时间:2023-11-29 02:24:51 26 4
gpt4 key购买 nike

我正在使用 UIGraphicsBeginImageContext 捕获屏幕,但图像质量不佳。任何人都可以帮助我如何将 UICollectionViewCell 保存为高质量图像以及如何在选择单元格时获取动态单元格的大小。

最佳答案

CollectionView 委托(delegate)方法将在单元格被选中时获取单元格,单元格图像将保存到项目文档目录中。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

UIGraphicsBeginImageContext(cell.bounds.size);

[cell.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"New ImageFolder"];

NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];

NSString *fileName = [stringPath stringByAppendingFormat:@"/image.jpg"];
NSData *data = UIImageJPEGRepresentation(newImage, 1.0);
[data writeToFile:fileName atomically:YES];

}

关于ios - 使用 UIGraphicsBeginImageContext 将 UICollectionViewCell 保存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684948/

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