Ios NSData对象因未知原因为空(保存图像)
好的,下面是我正在做的:Ios NSData对象因未知原因为空(保存图像),ios,image,file,save,nsdata,Ios,Image,File,Save,Nsdata,好的,下面是我正在做的: 加载imageView(带有图像),并允许用户选择添加过滤器 2a。如果他什么也不做,只是保存图片,图片就会保存 2b。如果他使用任何过滤器,图像视图将更新为过滤后的图片,然后使用相同的方法保存 我的问题:过滤图像后,为要保存的照片存储图像的NSData对象为零。 注: 两张图片都使用相同的方法保存 我正在使用CiColorControl修改图片 当用户过滤照片时,会将照片重置为其当前位置 原始状态,它保存 代码: viewdidLoad: 保存文件(视图中的图像)
- 两张图片都使用相同的方法保存
- 我正在使用CiColorControl修改图片
- 当用户过滤照片时,会将照片重置为其当前位置 原始状态,它保存
我已经没有主意了,我已经做了三天了,在我的一生中,我不知道到底出了什么问题。最重要的是,我知道这可能是非常愚蠢的事情。但是任何回答/评论都是受欢迎的:)Zil这里有一个保存照片的好方法
-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
NSString *lala=[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]];
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:lala]];
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
NSString *lala=[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]];
[UIImageJPEGRepresentation(image, 0.5) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:lala]];
}
}
唯一的区别是NSAtomicWrite参数
也许它可以帮助您。为了保留修改后的图像,您可以使用:
CIContext *context = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}];
CIImage *filteredImageData = [filter valueForKey:@"outputImage"];
CGImageRef cgImage = [context createCGImage:filteredImageData fromRect:[filteredImageData extent]];
UIImage *filteredImage = [UIImage imageWithCGImage:cgImage scale:[_photo scale] orientation:UIImageOrientationUp];
也许你必须在某个地方初始化照片。如果我知道filterImageWithBrightness的结果总是为零。对吗?不,就是这样。过滤后的图片显示正确(所以我猜它不是零…)。当我保存图像时,实际上只是UIImageJPEG表示为零。但只有当它是图像的过滤版本时,它的代码与我的代码几乎相同,只是你对文件名进行了测试,并在一个保存方法中获得了所有内容。我试过了,但没用(同样的问题,普通图片保存,过滤后的图片不保存。)
- (IBAction)softFilter:(id)sender {
_photo = [self filterImageWithBrightness:-0.15 AndContrast:0.95];
[self.imgPicture setImage:_photo];
}
-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
NSString *lala=[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]];
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:lala]];
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
NSString *lala=[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]];
[UIImageJPEGRepresentation(image, 0.5) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:lala]];
}
}
CIContext *context = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}];
CIImage *filteredImageData = [filter valueForKey:@"outputImage"];
CGImageRef cgImage = [context createCGImage:filteredImageData fromRect:[filteredImageData extent]];
UIImage *filteredImage = [UIImage imageWithCGImage:cgImage scale:[_photo scale] orientation:UIImageOrientationUp];