यहां एक टिप्पणी जोड़ने का तरीका बताया गया है (यानी एक EXIF उपयोगकर्ता टिप्पणी) स्विफ्ट में एक जेपीईजी छवि में। मुझे नहीं लगता कि यह इमेजियो फ्रेमवर्क का उपयोग करके किया जा सकता है।
कोड wrjpgcom
कमांड लाइन उपकरण से आता है, सी में लिखा गया है। यह libjpeg लाइब्रेरी का हिस्सा है। अधिक विस्तृत टिप्पणियों के लिए source code देखें (यह वास्तव में libjpeg का उपयोग नहीं करता है)।
हम सोफन से पहले नई टिप्पणी मार्कर डालेंगे। यह () नई टिप्पणी को पहले, मौजूदा टिप्पणियों के बजाए प्रकट होने का कारण बनता है; और (बी) यह सुनिश्चित करता है कि जेएफआईएफ विनिर्देश के अनुसार किसी भी जेएफआईएफ या जेएफXX मार्कर के बाद टिप्पणियां आती हैं।
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)
(Gist) जब उत्पादन जेपीईजी this वेबसाइट पर अपलोड की गई है
टिप्पणी दिखाया गया है:
हाँ आप केवल userComment संपत्ति उपयोग करने की आवश्यकता है, तो आप एक जोड़ सकते हैं '{"comment1": "yourcomment", "comment2": "yourcomment"}' – iphonic
@iphonic हां, जैसे ही स्ट्रिंग की बजाय संपत्ति में शब्दकोशकोष, मैंने पहले ही यह किया है। यह EXIF प्रकार मेटाडेटा में एक टिप्पणी जोड़ देगा लेकिन मुझे फ़ाइल प्रकार मेटाडेटा में जोड़ने की आवश्यकता है। कृपया इस लिंक को पढ़ें http://s000.tinyupload.com/?file_id=00209075531742860207 –
क्या आपने इसे आजमाया: 'url = URL (fileURLWithPath: "myfile.jpg") var resourceValues = URLResourceValues () // TODO: संसाधन मानों के लिए कुछ मान सेट करें url.setResourceValues (resourceValues) ' – Ramis