RubyMotionでExifを読む

久方ぶりにRubyMotionで小ネタ。軽くExifを読んでみようかと。

上記を参考にしたとりあえずの実験コード。app.rbにべたっと。

class AppDelegate
  attr_reader :path, :url, :meta, :exif
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    exif_export
    true
  end

  def exif_export
    @path = "#{NSBundle.mainBundle.resourcePath}/sample.jpg"
    @url = NSURL.fileURLWithPath(@path)
    cg_image = CGImageSourceCreateWithURL(@url, nil);
    if @meta = CGImageSourceCopyPropertiesAtIndex(cg_image, 0, nil)
      if @exif = @meta[KCGImagePropertyExifDictionary]
        @exif.each do |k, v|
          puts "#{k}:#{v}"
        end
      end
    end
  end
end

sample.jpgはresourcesの中に入れる。
Exifが全部削除されてるファイルを対象に試して小一時間首をかしげないように。そんな悲しい目にあうのは俺ひとりでたくさんだ。