简体   繁体   English

如何在iphone中以jpg或png格式保存图像?

[英]How to save image in jpg or png format in iphone?

I am taking an image from UIImageView and this image I am creating programmatically.Now I want to save this image in jpg or png format in Resource folder.Can anyone know how can I save image in jpg or png format?我正在从 UIImageView 获取图像,并且我正在以编程方式创建此图像。现在我想将此图像以 jpg 或 png 格式保存在 Resource 文件夹中。有人知道如何以 jpg 或 png 格式保存图像吗?

Thanks in advance!提前致谢!

This should work:这应该有效:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docs = [paths objectAtIndex:0];
NSString* path =  [docs stringByAppendingFormat:@"/image1.jpg"];

NSData* imageData = [NSData dataWithData:UIImageJPEGRepresentation(imageView.image, .8)];
NSError *writeError = nil;

if(![imageData writeToFile:path options:NSDataWritingAtomic error:&writeError]) {
    NSLog(@"%@: Error saving image: %@", [self class], [writeError localizedDescription]);
}

Careful.小心。 This line has a bug:此行有一个错误:

UIImageJPEGRepresentation(imageView.image, 80)

If you look at the header, the compression quality should be a float between 0 (most lossy) and 1 (least compression)如果您查看 header,压缩质量应该是介于 0(最有损)和 1(最小压缩)之间的浮点数

UIKIT_EXTERN NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);  // return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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