2011-05-26 53 views
0

我已經使用的Exif標籤上的JPEG文件的一些數據存儲在以下方式被:的iOS:在標記APP3訪問JPEG元

CGImageSourceRef source = CGImageSourceCreateWithURL(baseURL, NULL); 
NSDictionary *metadata = (NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source,0,NULL); 
NSMutableDictionary *metadataAsMutable = [[metadata mutableCopy]autorelease]; 
NSMutableDictionary *EXIFDictionary = [[[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary]mutableCopy]autorelease]; 
[EXIFDictionary setObject:[NSString stringWithFormat:@"%d",tag] forKey:(NSString *)kCGImagePropertyExifUserComment]; 

現在,我想用一個自定義應用程序標記(APP3在0xFFE3)而不是Exif標記。

(參見 - http://www.ozhiker.com/electronics/pjmt/jpeg_info/app_segments.html

有人能指出我在正確的方向。 PS:我爲這個應用程序使用了越獄的ipad公司。

回答

0

好吧,似乎我們必須通過文件處理程序的方式來做到這一點。這是我所做的,儘管可能有更好的方法來做到這一點。

創建一個文件句柄:

NSString *filePath = currentImageObject.myFilePath; 
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath]; 
if(!fileHandle){ 
    return; 
} 
[fileHandle seekToEndOfFile]; 
unsigned long long eofOffset = [fileHandle offsetInFile]; 

然後遍歷文件內容,直到你找到想要的標籤:

BOOL markerFound = NO; 
BOOL dqtFound = NO; 
while ((!markerFound) && (!dqtFound) && ([fileHandle offsetInFile] < eofOffset)) { 
     currentOffset += 1; 
     [fileHandle seekToFileOffset:currentOffset]; 
     NSData *markerData = [fileHandle readDataOfLength:1]; 
     currentOffset += 1; 
     NSInteger markerValue = (unsigned char)*(unsigned char *)[markerData bytes]; 

     if (0xe0 == markerValue) { 
      currentOffset += 14; 
      [fileHandle seekToFileOffset:currentOffset]; 

      NSData *xThumbnailData = [fileHandle readDataOfLength:1]; 
      currentOffset += 1; 
      NSData *yThumbnailData = [fileHandle readDataOfLength:1]; 
      currentOffset += 1; 
      NSInteger xThumbnail = (unsigned char)*(unsigned char *)[xThumbnailData bytes]; 
      NSInteger yThumbnail = (unsigned char)*(unsigned char *)[yThumbnailData bytes]; 
      NSInteger thumbnailSize = 3 * xThumbnail * yThumbnail; 
      currentOffset += thumbnailSize; 
      [fileHandle seekToFileOffset:currentOffset]; 
     } else if (0xe3 == markerValue) { 
      markerFound = YES; 
      break; 
     } else if (0xdb == markerValue) { 
      dqtFound = YES; 
      break; 
     } else { 
      NSData *lengthData = [fileHandle readDataOfLength:2]; 
      currentOffset += 2; 
      NSInteger length = (unsigned short)*(unsigned short *)[lengthData bytes]; 
      length = NSSwapBigShortToHost(length); 
      length -= 2; 
      currentOffset += length; 
      [fileHandle seekToFileOffset:currentOffset]; 
     } 
    } 

這給你的偏移量APP3標記,櫃面你需要添加你自己的app3標記,你可以使用類似的方法。