gpt4 book ai didi

iphone - 将带有 exif 的原始图像从 ALAsset 对象写入文档目录文件夹

转载 作者:行者123 更新时间:2023-12-03 21:15:29 28 4
gpt4 key购买 nike

我想将 Assets 图像数组直接保存到带有 EXIF 的文档目录我尝试了 PNG 转换、jpeg 转换,但没有成功它只是用 jpg 或 png 创建新图像(丢失 exif)

我有一段时间看到将 NSData 直接保存到文件夹以保留 EXIF 不知道如何

我使用

从 ALAsset 对象结果中获取元数据
NSDictionary *metadata = [[result defaultRepresentation] metadata];

另一个包含所有图像列表的资源数组

ALAssetsGroupEnumerationResultsBlock assetEnumerator = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != NULL) {
[assets addObject:result];

;
NSLog(@"assets %i",[assets count]);
self.progressView.progress = (float)index / ([assets count]-1);
}

将图像保存到文档目录文件夹

-(void)saveImagesToDocumentDirectory{
NSLog(@"assets count %i",[assets count]);

for(int i=0;i<[assets count];i++)
{
currentImage = [UIImage imageWithCGImage:[[[assets objectAtIndex:i] defaultRepresentation] fullResolutionImage]];

[self saveImage:currentImage withImageName:[NSString stringWithFormat:@"Images %d",i]];
} }

- (void)saveImage:(UIImage*)image withImageName:(NSString*)imageName {
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
// NSData *imageData = UIImageJPEGRepresentation(image,1.0);
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyFolder"];
NSString *fullPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)

NSLog(@"image saved");
}

最佳答案

这是我能够在文档目录文件夹中写入所有图像且 exif 保持不变的方法,希望这会帮助其他用户

-(void)writingOutImage{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *documentdataPath = [documentsDirectory stringByAppendingPathComponent:@"MyFolder"];
NSLog(@"documentdataPath %@",documentdataPath);
for (int j=0; j<[assets count]; j++) {

ALAssetRepresentation *representation = [[assets objectAtIndex:j] defaultRepresentation];
NSString* filename = [documentdataPath stringByAppendingPathComponent:[representation filename]];

[[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
NSOutputStream *outPutStream = [NSOutputStream outputStreamToFileAtPath:filename append:YES];
[outPutStream open];

long long offset = 0;
long long bytesRead = 0;

NSError *error;
uint8_t * buffer = malloc(131072);
while (offset<[representation size] && [outPutStream hasSpaceAvailable]) {
bytesRead = [representation getBytes:buffer fromOffset:offset length:131072 error:&error];
[outPutStream write:buffer maxLength:bytesRead];
offset = offset+bytesRead;
}
[outPutStream close];
free(buffer);
}
}

关于iphone - 将带有 exif 的原始图像从 ALAsset 对象写入文档目录文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16542767/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com