【问题标题】:Save signature image in png format in ios在ios中以png格式保存签名图像
【发布时间】:2023-03-05 12:58:01
【问题描述】:

我有用于签名的可绘制文本, 我希望此上下文以具有特定路径的 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;
}

【问题讨论】:

标签: ios objective-c nsdata


【解决方案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];

【讨论】:

  • 感谢所有人..谁帮助我...认真节省很多时间..@Abhishek 和 @Madhu
【解决方案2】:

这样试试

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

【讨论】:

  • @madhumita 感谢您的帮助。但我仍然无法保存图像
  • 您是否检查过您的 _SignatureView.signatureImage 是否有图像参考?
  • :-我在下一个视图控制器中检查了我的 iamge 显示,但它保存在哪里,我不知道。
  • 我在我的 mac 中签入了文档,但没有图像..给我解决方案
  • 参考此链接并打印保存的图像路径。在该路径中检查后。stackoverflow.com/questions/11219202/…
猜你喜欢
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 2019-12-21
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
相关资源
最近更新 更多