Ios 在图像文件头上添加注释
我想在图像文件头中添加注释。因为我可以在UserComment下添加关于EXIF元数据的注释,但我想在图像文件头中添加多个注释Ios 在图像文件头上添加注释,ios,swift,file,uiimage,metadata,Ios,Swift,File,Uiimage,Metadata,我想在图像文件头中添加注释。因为我可以在UserComment下添加关于EXIF元数据的注释,但我想在图像文件头中添加多个注释 我应该使用哪个框架?对于UserComment,我使用的是ImageIO框架。以下是如何在Swift中向JPEG图像添加注释(即而不是EXIF UserComment)。我不认为使用ImageIO框架可以做到这一点 代码来自于用C编写的wrjpgcom命令行工具。它是libjpeg库的一部分。查看以获取更详细的注释(它实际上不使用libjpeg) 我们将在SOFn之前插
我应该使用哪个框架?对于UserComment,我使用的是ImageIO框架。以下是如何在Swift中向JPEG图像添加注释(即而不是EXIF UserComment)。我不认为使用ImageIO框架可以做到这一点 代码来自于用C编写的
wrjpgcom
命令行工具。它是libjpeg库的一部分。查看以获取更详细的注释(它实际上不使用libjpeg)
我们将在SOFn之前插入新的注释标记。这(a)导致新注释出现在现有注释之后,而不是之前;并且(b)确保注释位于JFIF规范要求的任何JFIF或JFXX标记之后
<代码>导入基础
func addJPEGComment(到jpegData:inout数据,comment:String){
//查找第一个SOF标记或EOI的索引
让我们看看[UInt8]=[
0xC0、0xC1、0xC2、0xC3、0xC5、0xC6、,
0xC7、0xC9、0xCA、0xCB、0xCD、0xCE、,
0xCF,0xD9//EOI
]
var firstSOFRange:范围?
对于SOF标记中的标记{
如果let range=jpegData.range(of:Data(字节:[0xFF,标记])){
firstSOFRange=范围
打破
}
}
防护罩让firstSOFIndex=firstSOFRange?.lowerBound
else{fatalError(“未找到SOF或EOI标记”)}
//创建注释字节数组
让length=comment.lengthOfBytes(使用:.utf8)+2
设l1=UInt8(长度>>8)和0xFF)
设l2=UInt8(长度&0xFF)
让commentArray=[0xFF,0xFE/*COM标记*/,l1,l2]+[UInt8](comment.utf8)
//将注释数组插入到图像数据对象中
jpegData.insert(contentsOf:commentArray,at:firstSOFIndex)
}
guard let jpegURL=Bundle.main.url(用于资源:“no_com”,扩展名为:“jpg”)
else{fatalError(“找不到文件”)}
guard var jpegData=尝试?数据(内容:jpegURL)
else{fatalError(“无法读取文件”)}
添加JPEG注释(至:&jpegData,“这是JPEG注释”)
guard let jpegOutputURL=尝试吗?FileManager.default.url(用于:.documentDirectory,位于:.userDomainMask中,适用于:nil,创建:false)
else{fatalError(“未创建目标URL”)}
尝试JPEG数据。写入(到:JPEG输出URL)
打印(JPEG输出URL)
()
将输出JPEG上载到网站时会显示注释:
是的您只需要使用userComment属性,您可以向该属性添加字典,而不是像
{“comment1”:“yourcoment”,“comment2”:“yourcoment”}
@iphone是的,我已经这样做了。这将在EXIF类型元数据中添加注释,但我需要在元数据的文件类型中添加注释。请阅读此链接您是否尝试过此链接:let url=url(fileURLWithPath:“myfile.jpg”)var resourceValues=URLResourceValues()//TODO:将一些值设置为resourceValues url.setResourceValues(resourceValues)
您尝试过照片框架吗?图像文件头是什么意思?你说的是图像文件中的元数据,还是实际图像文件中不包含的其他类型的元数据?
import Foundation
func addJPEGComment(to jpegData: inout Data, _ comment: String) {
// find index of first SOF marker, or EOI
let sofMarkers: [UInt8] = [
0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6,
0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE,
0xCF, 0xD9 // EOI
]
var firstSOFRange: Range<Data.Index>?
for marker in sofMarkers {
if let range = jpegData.range(of: Data(bytes: [ 0xFF, marker ])) {
firstSOFRange = range
break
}
}
guard let firstSOFIndex = firstSOFRange?.lowerBound
else { fatalError("No SOF or EOI marker found.") }
// create comment byte array
let length = comment.lengthOfBytes(using: .utf8) + 2
let l1 = UInt8((length >> 8) & 0xFF)
let l2 = UInt8(length & 0xFF)
let commentArray = [ 0xFF, 0xFE /* COM marker */, l1, l2 ] + [UInt8](comment.utf8)
// insert comment array into image data object
jpegData.insert(contentsOf: commentArray, at: firstSOFIndex)
}
guard let jpegURL = Bundle.main.url(forResource: "no_com", withExtension: "jpg")
else { fatalError("File not found.") }
guard var jpegData = try? Data(contentsOf: jpegURL)
else { fatalError("File could not be read.") }
addJPEGComment(to: &jpegData, "This is a JPEG comment.")
guard let jpegOutputURL = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("with_com.jpg")
else { fatalError("Destination URL not created") }
try jpegData.write(to: jpegOutputURL)
print(jpegOutputURL)