2017-05-06 1 views
1

) 앱의 경우 회사의 모든 동료와 vcf-File을 구문 분석하려고합니다. 그들 중 일부는 실제 사진이없고 자동으로 더미 사진이 삽입됩니다. 이제 앱이 미래의 증거가 될 수 있도록 지금 당장 작동 할 500x500의 해상도를 확인하고 싶지 않습니다. vcf 생성을 담당하는 부서의 아이디어는 항상 사용하는 더미 사진 자료 파일에 주석을 추가하는 것이 었습니다. 나는 스위프트에 그 읽기 시도,하지만 당신은 내 테스트 놀이터 코드에서 볼 수 있듯이 운이 없다 :PNG 파일에서 EXIF ​​데이터 읽기 (

import UIKit 
import ImageIO 

let photo = UIImage(named: "bild")! 
let photoData = UIImagePNGRepresentation(photo)! 
let base64String = photoData.base64EncodedString() 

let photoSource = CGImageSourceCreateWithData(photoData as CFData, nil)! 
for (key, value) in CGImageSourceCopyPropertiesAtIndex(photoSource, 0, nil) as! [String : Any] { 
    print("\(key): \(value)") 
} 

출력은 :

PixelWidth: 500 
Depth: 8 
ProfileName: sRGB IEC61966-2.1 
HasAlpha: 1 
ColorModel: RGB 
{PNG}: { 
    Chromaticities =  (
     "0.3127", 
     "0.329", 
     "0.64", 
     "0.33", 
     "0.3", 
     "0.6000000000000001", 
     "0.15", 
     "0.06" 
    ); 
    Gamma = "0.45455"; 
    InterlaceType = 0; 
    sRGBIntent = 0; 
} 
PixelHeight: 500 

터미널에서 exiftool의 출력은 한편 같은 이미지에 표시() 특히 User CommentDocument Name (사용자 정의 필드를 참조하십시오

➔ exiftool bild.png 
ExifTool Version Number   : 10.50 
File Name      : bild.png 
Directory      : . 
File Size      : 4.2 kB 
File Modification Date/Time  : 2017:05:06 12:51:23+02:00 
File Access Date/Time   : 2017:05:06 12:51:24+02:00 
File Inode Change Date/Time  : 2017:05:06 12:51:23+02:00 
File Permissions    : rw-r--r-- 
File Type      : PNG 
File Type Extension    : png 
MIME Type      : image/png 
Image Width      : 500 
Image Height     : 500 
Bit Depth      : 8 
Color Type      : Palette 
Compression      : Deflate/Inflate 
Filter       : Adaptive 
Interlace      : Noninterlaced 
Palette       : (Binary data 477 bytes, use -b option to extract) 
Transparency     : 0 
Background Color    : 0 
Pixels Per Unit X    : 2835 
Pixels Per Unit Y    : 2835 
Pixel Units      : meters 
Modify Date      : 2017:05:05 08:04:36 
Exif Byte Order     : Big-endian (Motorola, MM) 
Document Name     : dummy 
X Resolution     : 72 
Y Resolution     : 72 
Resolution Unit     : inches 
Y Cb Cr Positioning    : Centered 
Exif Version     : 0231 
Components Configuration  : Y, Cb, Cr, - 
User Comment     : dummy 
Flashpix Version    : 0100 
Color Space      : Uncalibrated 
Image Size      : 500x500 
Megapixels      : 0.250 

가 이미 kCGImagePropertyExifU를 사용하여 사용자 내용에 접근 시도 serComment,이 전무 반환하고 나는 위의 코드는 예상대로 일 경우에만, 어떤 값을 반환 생각하지만 :

let userComment = dict[kCGImagePropertyExifUserComment as String] // User Comment is set --> but this returns nil 
let pixelWidth = dict[kCGImagePropertyPixelWidth as String] // As a reference that this does normally work --> shows 500 as expected 

당신이 방법에 읽을 수있는 이미지에 주석을 추가하는 제안이 있습니까 신속한 코드?

+1

PNG에는 EXIF가 포함되어 있지 않습니다. PNG 형식에는 'exiftool'이 읽을 수있는 자체 메타 데이터 청크가 있지만 'CoreImage'는 읽을 필요가 없습니다. 그러나'libpng'를 통해 그것들을 읽는 것은 그렇게 어렵지 않습니다. –

+0

좋아요, 왜 그걸 읽을 수 없는지 설명하지만 아이폰 OS 앱에서'libpng'을 가져 와서 아이폰에서 메타 데이터에 접근 할 수있는 방법은 뭔가요? –

+1

코코아포드는 어때요? https://cocoapods.org/pods/libpng –

답변

0

이미지를 만들고 메타 데이터가있는 PNG로 저장 한 다음 파일에서 해당 메타 데이터를 검색하는 방법을 보여주는 완전한 예입니다. 이것을 iOS 운동장에 붙여 넣을 수 있어야합니다.

//: Playground - noun: a place where people can play 

import UIKit 
import ImageIO 
import MobileCoreServices 

var str = "Hello, playground" 

if let image = createImage() { 
    let pngDictionary : NSDictionary =  [ 
     kCGImagePropertyPNGTitle : "Smile for the Camera", 
     kCGImagePropertyPNGAuthor : "Smiles-R-Us", 
     kCGImagePropertyPNGCopyright : "©2017 Smiles-R-Us", 
     kCGImagePropertyPNGCreationTime : String(describing: Date()), 
     kCGImagePropertyPNGDescription : "Have a Nice Day!" 
    ] 

    let imageMetadata : NSDictionary = [ kCGImagePropertyPNGDictionary : pngDictionary ] 

    let tempURL = FileManager.default.temporaryDirectory 
    let filePath = tempURL.appendingPathComponent("Smile.png") as NSURL 

    let imageDestination = CGImageDestinationCreateWithURL(filePath, kUTTypePNG, 1, nil) 
    if let destination = imageDestination { 
     CGImageDestinationAddImage(destination, image.cgImage!, imageMetadata) 
     CGImageDestinationFinalize(destination) 
    } 

    if let imageSource = CGImageSourceCreateWithURL(filePath, nil) { 
    print (CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)) 
    } 

    print(filePath) 
} 

func createImage() -> UIImage? { 
    let bounds = CGRect(x: 0, y: 0, width: 200, height: 200) 
    UIGraphicsBeginImageContext(bounds.size) 
    if let cgContext = UIGraphicsGetCurrentContext() { 
     let inset = bounds.insetBy(dx: 20, dy: 20) 

     cgContext.clear(bounds) 
     cgContext.saveGState() 
     cgContext.setStrokeColor(UIColor.black.cgColor) 
     cgContext.setFillColor(UIColor.black.cgColor) 
     cgContext.setLineWidth(2.0) 
     cgContext.strokeEllipse(in: inset) 
     let eyeLevel = inset.maxY - (inset.height * 0.618) 
     cgContext.fillEllipse(in: CGRect(x: inset.minX + inset.width * 0.3, 
             y: eyeLevel, width: 10, height: 10)) 
     cgContext.fillEllipse(in: CGRect(x: inset.minX + inset.width * 0.6, 
             y: eyeLevel, width: 10, height: 10)) 
     cgContext.addArc(center: CGPoint(x:inset.midX, y:inset.midY), radius: (inset.width/2.0 - 20), startAngle: 2.61, endAngle: 0.52, clockwise: true) 
     cgContext.strokePath() 
    } 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return image 
} 
+0

놀이터에 머물고 파일에서 즉시 읽는 한 실제로 작동합니다. 생성 된 Smile.png을 가져 가면 놀이터의 ressources 폴더로 드래그하고 Xcode가 불필요하다고 판단한 모든 필드를 제거한 파일에서 메타 데이터를 읽습니다. 따라서 슬프게도 응용 프로그램의 연락처 파일에 사진을 저장하는 솔루션으로 사용할 수 없습니다. –