1

Is there a way I can convert the view of a UITableViewCell or UICollectionViewCell into an UIImageView which looks the same?

I want to be able to transform the snapshot of the cell into a UIImageView, in order to be able to animate the UIImageView. The cell itself can remain the same.

Charles
  • 50,943
  • 13
  • 104
  • 142
cannyboy
  • 24,180
  • 40
  • 146
  • 252

2 Answers2

4

You can use the following code:

UIGraphicsBeginImageContextWithOptions(cell.bounds.size, cell.opaque, 0.0);
[cell.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *cellImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Matt
  • 2,391
  • 2
  • 17
  • 18
0

On a retina device you should use this

CGFloat scale = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContextWithOptions(frame.size, YES, scale);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The second parameter in UIGraphicsBeginImageContextWithOptions should be NO if the cell has transparent parts.

iNevs
  • 99
  • 5