简体   繁体   中英

Save signature image in png format in ios

I have drawable text which is used for signature , I want this context save in png format with a specific path but I could not do this.I want my draw image save in png format and I can use any where

**Code for png format and path**

NSData *imageData = UIImagePNGRepresentation(_SignatureView.signatureImage);

 NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/imageName.png"];

[imageData writeToFile:imagePath atomically:YES];

_SignatureView is a property and signatureImage is a method 

**code of signature image method**

- (UIImage *)signatureImage
{
    if (!self.hasSignature)
        return nil;


    UIImage *screenshot = [self snapshot];


    return screenshot;
}
NSString *docPath=  @"/Users/.../.../";
NSString *filePath=[docPath stringByAppendingPathComponent:@"PdfFileName.pdf"];

NSMutableData pdfData=[NSMutableData data];
CGRect bounds = CGRectMake(0, 0, 612, 792);
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);

CGContextRef pdfContext = UIGraphicsGetCurrentContext();
UIGraphicsBeginPDFPage();

[YourSignature.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
[pdfData writeToFile:filePath atomically:YES];

Try like this

- (void)saveImage: (UIImage*)image
  {
    if (image != nil)
    {
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
       NSUserDomainMask, YES);
       NSString *documentsDirectory = [paths objectAtIndex:0];
       NSString* path = [documentsDirectory stringByAppendingPathComponent:
      [NSString stringWithString: @"test.png"] ];
      NSData* data = UIImagePNGRepresentation(image);
      [data writeToFile:path atomically:YES];
      }
  }

and to get the image use the following code

 - (UIImage*)loadImage
 {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
   NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString* path = [documentsDirectory stringByAppendingPathComponent:
   [NSString stringWithString: @"test.png"] ];
   UIImage* image = [UIImage imageWithContentsOfFile:path];
   return image;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM