在ios中以png格式保存签名图像

时间:2016-05-04 06:59:24

标签: ios objective-c nsdata

我有可用于签名的可绘制文本, 我希望这个上下文保存为具有特定路径的png格式,但我不能这样做。我希望我的绘图图像以png格式保存,我可以使用任何地方

**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;
}

2 个答案:

答案 0 :(得分:1)

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];

答案 1 :(得分:0)

试试这个

- (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];
      }
  }

并获取图像使用以下代码

 - (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;
 }