Posted By

ezeewoo on 11/19/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tintenklecks


Capture UIImage and write png


 / Published in: Objective C
 

  1. - (NSString *)saveBaseDir {
  2. NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  3. NSUserDomainMask,
  4. YES);
  5. return [array objectAtIndex:0];
  6. }
  7.  
  8. - (BOOL) captureView:(UIView *)aview name:(NSString *)filename {
  9.  
  10. UIGraphicsBeginImageContext(aview.frame.size);
  11. [aview.layer renderInContext:UIGraphicsGetCurrentContext()];
  12. UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
  13. UIGraphicsEndImageContext();
  14.  
  15. NSString *saveFileName = filename;
  16. NSString *saveFilePath = [[self saveBaseDir] stringByAppendingPathComponent:saveFileName];
  17.  
  18. NSData *data = UIImagePNGRepresentation(viewImage);
  19. NSError *error = nil;
  20. [data writeToFile:saveFilePath options:NSDataWritingAtomic error:&error];
  21. if (error) {
  22. NSLog(@"Error png file:%@ error=%@", saveFileName, [error description]);
  23. return NO;
  24. } else {
  25. NSLog(@"Success png file:%@", saveFileName);
  26. return YES;
  27. }
  28. }
  29.  
  30.  

Report this snippet  

You need to login to post a comment.