将图像元数据写入iOS中的JPG / PNG文件

时间:2021-01-14 21:23:53

Is there any way to write a UIImage to a JPG/PNG file and then append metadata to it? I know you can use:

有没有办法将UIImage写入JPG / PNG文件,然后将元数据附加到它?我知道你可以用:

writeImageDataToSavedPhotosAlbum:metadata:completionBlock

to do this in the Saved Photos, but how do you do the same thing directly to a file? I can't seem to find any way to do this. I know that UIImagePNGRepresentation() and UIImageJPGRepresentation() will give you NSData that you can use to write the file, but there's no way to append/replace the metadata in the file.

在保存的照片中执行此操作,但是如何直接对文件执行相同的操作?我似乎无法找到任何方法来做到这一点。我知道UIImagePNGRepresentation()和UIImageJPGRepresentation()将为您提供可用于编写文件的NSData,但无法在文件中追加/替换元数据。

Any ideas?

2 个解决方案

#1


3  

Looks like a duplicate.

看起来像是重复的。

To add metadata to a UIImage you can use the ImageIO framework You can create a CGImageDestination object from a UIImage, add metadata to it using CGImageDestinationSetProperties and then get the raw data (which includes the compressed image and the metadata) from it.

要向UIImage添加元数据,您可以使用ImageIO框架您可以从UIImage创建CGImageDestination对象,使用CGImageDestinationSetProperties向其添加元数据,然后从中获取原始数据(包括压缩图像和元数据)。

#2


-7  

@rekle Check it out this code is for writing the photo in to file

@rekle查看此代码是用于将照片写入文件

NSData *imageData = UIImagePNGRepresentation(self.imageView.image);
            NSData *myImage=[[NSData alloc]initWithData:imageData];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSLog(@"%@",documentsDirectory);

            NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",camtitle.text]];
            [myImage writeToFile:fullPathToFile atomically:YES];
            NSLog(@"%@",fullPathToFile);
            [myImage release

];

#1


3  

Looks like a duplicate.

看起来像是重复的。

To add metadata to a UIImage you can use the ImageIO framework You can create a CGImageDestination object from a UIImage, add metadata to it using CGImageDestinationSetProperties and then get the raw data (which includes the compressed image and the metadata) from it.

要向UIImage添加元数据,您可以使用ImageIO框架您可以从UIImage创建CGImageDestination对象,使用CGImageDestinationSetProperties向其添加元数据,然后从中获取原始数据(包括压缩图像和元数据)。

#2


-7  

@rekle Check it out this code is for writing the photo in to file

@rekle查看此代码是用于将照片写入文件

NSData *imageData = UIImagePNGRepresentation(self.imageView.image);
            NSData *myImage=[[NSData alloc]initWithData:imageData];
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSLog(@"%@",documentsDirectory);

            NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",camtitle.text]];
            [myImage writeToFile:fullPathToFile atomically:YES];
            NSLog(@"%@",fullPathToFile);
            [myImage release

];