首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式从iPhoto库中读取

以编程方式从iPhoto库中读取
EN

Stack Overflow用户
提问于 2011-12-06 21:57:42
回答 2查看 4.5K关注 0票数 5

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

有没有一种很好的/简单的方法来做到这一点,或者我必须手动读取iPhoto用户数据的包结构?

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-14 23:35:25

你可以用NSAppleScript做到这一点。这是我的应用程序中的一些复制/粘贴,只是为了展示一下想法而做了一些修改。

代码语言:javascript
复制
    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];

您可以执行相同的操作来查找图像

代码语言:javascript
复制
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];
票数 5
EN

Stack Overflow用户

发布于 2013-11-04 18:30:38

如果在App Store上发布应用程序,您现在需要使用沙盒,这将停止以前的AppleScript方法( iPhoto应用程序启动,但返回一个空集)。

XML库由一个包含照片、数据库和iPhoto文件的目录结构组成。每个iPhoto版本的内容都会有所不同,因此手动访问这些文件时要小心。

如果您只想要专辑的详细信息,可以解析文件AlbumData.xml

如果你想要照片,你可以浏览“大师”文件夹。文件结构跟随日期,而不是iPhoto中配置的集。

在这里可以找到关于iPhoto库内部的更多信息:http://www.fatcatsoftware.com/iplm/Help/iphoto%20library%20internals.html

大多数数据库都是SQLite格式的,因此可以通过Objective C以编程方式访问,尽管您可以再次预期不同版本的iPhoto之间的模式变化。感兴趣的主要数据库是数据库/apdb中的Library.apdb和Properties.apdb。

如果你仍然想使用Apple Script方法,这里是之前答案的一个版本,其中包括Apple script执行部分:

代码语言:javascript
复制
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"iPhoto\" to properties of albums"];
NSAppleEventDescriptor *d = [script executeAndReturnError:nil];

NSLog(@"photo library count: %ld", (long)[d numberOfItems]);

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

    NSString *albumName = [[albumDesc descriptorForKeyword:'pnam'] stringValue];
    NSLog(@"%@", albumName);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8401054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档