我正在使用UIGraphicsBeginImageContext捕获屏幕,但图像质量不佳。任何人都可以帮助我如何将UICollectionViewCell保存为高质量图像,以及如何在选择单元格时获取动态单元格的大小。
答案 0 :(得分:1)
CollectionView委托方法会在选中单元格后获取单元格,单元格图像将保存到项目文档目录中。
-(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];
}