objective-c - 以编程方式从 iPhoto 图库中读取

标签 objective-c xcode macos cocoa iphoto

我想创建一个连接到 iPhoto 图库的应用程序。所以现在我想从图书馆阅读事件和图片本身。

是否有一种优雅/简单的方法来执行此操作,还是我必须手动读取 iPhoto 用户数据的捆绑结构?

到目前为止我只找到了一个拍照者:Is there a UIImagePicker for the Mac Desktop

更新:我找到了另一个相关的 SO 帖子:Selecting iPhoto images within a cocoa application

最佳答案

你可以用 NSAppleScript 做到这一点。这是从我的应用程序中复制/粘贴的一些内容,经过一些改动只是为了展示这个想法。

    NSAppleEventDescriptor d = .. compile this script ..
        @"tell application \"iPhoto\" to properties of albums"

    for (int i = 0; i < [d numberOfItems]; i++)
    {
        NSAppleEventDescriptor *albumDesc = [d descriptorAtIndex:i];

        // <NSAppleEventDescriptor: 'ipal'{ 
        //  'ID  ':4.265e+09, 
        //  'purl':'utxt'("http://www.flickr.com/photos/..."), 
        //  'pnam':'utxt'("Vacation"), 
        //  'alTy':'pubs', 
        //  'alCh':[  ], 
        //  'alPx':'msng' }>

        NSString *albumName = [[albumDesc descriptorForKeyword:'pnam'] stringValue];
        NSString *albumId = [[albumDesc descriptorForKeyword:'ID  '] stringValue];

你可以做同样的事情来找到图片

NSString *scp = 
    [NSString stringWithFormat:@"tell application \"iPhoto\" to properties of photos of album id %@",
     [album objectForKey:@"id"]];

NSAppleEventDescriptor *d = ... compile scp ...

// 1 based!?
for (int i = 1; i <= [d numberOfItems]; i++)
{
    NSAppleEventDescriptor *photoDesc = [d descriptorAtIndex:i];

    // Yes.. this happens.  Not sure why?!
    if (!photoDesc)
        continue;

    // <NSAppleEventDescriptor: 'ipmr'{ 
    // 'pnam':'utxt'("IMG_0058.JPG"), 
    // 'pwid':768, 
    // 'pdim':[ 768, 1024 ], 
    // 'alti':1.79769e+308, 
    // 'filn':'utxt'("3133889525_10975ba071_b.jpg"), 
    // 'ipth':'utxt'("/Users/lagnat/Pictures/iPhoto Library/Masters/2010/11/10/20101110-002341/3133889525_10975ba071_b.jpg"), 
    // 'idat':'ldt '($F57C69C500000000$), 
    // 'rate':0, 
    // 'titl':'utxt'("IMG_0058.JPG"), 
    // 'phit':1024, 
    // 'itpt':'utxt'("/Users/lagnat/Pictures/iPhoto Library/Thumbnails/2010/11/10/20101110-002341/3133889525_10975ba071_b.jpg.jpg"), 
    // 'ID  ':4.295e+09, 
    // 'lati':'msng', 
    // 'pcom':'utxt'(""), 
    // 'opth':'utxt'("/Users/lagnat/Pictures/iPhoto Library/Masters/2010/11/10/20101110-002341/3133889525_10975ba071_b.jpg"), 
    // 'lngt':'msng', 
    // 'tiln':'utxt'("3133889525_10975ba071_b.jpg.jpg") }>

    NSString *path = [[photoDesc descriptorForKeyword:'ipth'] stringValue];
    NSString *imgname = [[photoDesc descriptorForKeyword:'pnam'] stringValue];

关于objective-c - 以编程方式从 iPhoto 图库中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8401054/

相关文章:

ios - 如何在 swift 中以编程方式创建 UITextField 事件?

ios - 如何在 Xcode 7 和 Swift 2 中创建圆角矩形标签

MacOS 需要不同的操作系统版本与 Info.plist

macos - MacBook 触控板手势 : Move Windows like in Minority Report

ios - CollectionView 委托(delegate)方法未从 TableViewCell 调用

macos - 在 OSX/Unix 上启动一个不继承文件/端口的子进程

iPhone - 带有双引号转义字符的 Localizable.strings

objective-c - ARC时代的属性(property)与ivar

iphone - 第一次单元格可见时未绘制 UIImageView 阴影

ios - Xcode 多线程违规“留给我们的就是荣誉 cocoa ”