2011-12-06 23 views
5

Tôi muốn tạo một Ứng dụng kết nối với Thư viện iPhoto. Vì vậy, bây giờ tôi muốn đọc các sự kiện và hình ảnh mình từ thư viện.Đọc từ iPhoto Thư viện theo chương trình

Có cách nào thanh lịch/dễ dàng để thực hiện việc này hay tôi có phải đọc thủ công Cấu trúc gói của Dữ liệu người dùng iPhoto không?

Cho đến nay tôi đã chỉ tìm thấy một bức tranh taker: Is there a UIImagePicker for the Mac Desktop

Cập nhật: Tôi tìm thấy một SO liên quan bài: Selecting iPhoto images within a cocoa application

Trả lời

5

Bạn có thể làm điều đó với NSAppleScript. Đây là một số bản sao/dán từ ứng dụng của tôi, bị hack một chút chỉ để hiển thị ý tưởng.

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]; 

Bạn có thể làm điều tương tự để tìm những hình ảnh

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]; 
1

Nếu phát hành ứng dụng trên App Store bạn đang yêu cầu hiện nay cần thiết để sử dụng Sandbox, này dừng phương pháp AppleScript trước khi làm việc (ứng dụng iPhoto khởi chạy nhưng một tập rỗng được trả lại).

Thư viện iPhoto bao gồm cấu trúc thư mục chứa ảnh, cơ sở dữ liệu và tệp XML. Nội dung thay đổi với mỗi phiên bản iPhoto vì vậy hãy cẩn thận nếu tự truy cập các tệp này.

Nếu bạn chỉ muốn các chi tiết album bạn có thể phân tích các tập tin AlbumData.xml

Nếu bạn muốn hình ảnh bạn có thể duyệt các thư mục Masters. Cấu trúc tệp theo ngày chứ không phải bởi các bộ cấu hình trong iPhoto.

Thông tin thêm có thể được tìm thấy trên bên trong của thư viện iPhoto đây: http://www.fatcatsoftware.com/iplm/Help/iphoto%20library%20internals.html

Phần lớn các cơ sở dữ liệu có định dạng SQLite và như vậy có thể được truy cập theo chương trình thông qua Objective C, mặc dù một lần nữa bạn có thể mong đợi những thay đổi schema giữa các phiên bản iPhoto khác nhau. Cơ sở dữ liệu quan tâm chính là Library.apdb và Properties.apdb trong Database/apdb.


Nếu bạn vẫn muốn sử dụng phương pháp của Apple Script, đây là một phiên bản của câu trả lời trước với phần thực hiện kịch bản của Apple bao gồm:

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); 
} 
Các vấn đề liên quan