跨设备的iCloud文件存储/交换

时间:2015-10-06 18:18:36

标签: ios icloud document

我正在尝试将数据存储到iCloud文档,并在使用同一帐户登录的其他设备上读取它。

我可以检测到iCloud,将数据发送到iCloud并在同一设备上将其读回。我可以从第二个设备检测到该文件,但是我收到错误' NSCocoaDomainError - 代码260'当我尝试阅读或复制文件时。

文件存储代码:

#include <opencv2\opencv.hpp>
using namespace cv;


int main()
{
    Mat1b grayImg = imread("path_to_image", IMREAD_GRAYSCALE);

    Mat1f data(grayImg.rows*grayImg.cols, 1);
    for (int r = 0; r < grayImg.rows; r++)
    {
        for (int c = 0; c < grayImg.cols; c++)
        {
            data(r*grayImg.cols + c) = float(grayImg(r, c));

        }
    }

    // Or, equivalently
    //Mat1f data;
    //grayImg.convertTo(data, CV_32F);
    //data = data.reshape(1, 1).t();


    // I should have obtained the vector in p, so now I want to supply it to k-means: 
    int K = 8;
    Mat1i bestLabels(data.size(), 0); // integer matrix of labels
    Mat1f centers;                    // float matrix of centers
    cv::kmeans(data, K, bestLabels,
        cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0),
        3, cv::KMEANS_PP_CENTERS, centers);


    // Show results
    Mat1b result(grayImg.rows, grayImg.cols);
    for (int r = 0; r < result.rows; ++r)
    {
        for (int c = 0; c < result.cols; ++c)
        {
            result(r, c) = static_cast<uchar>(centers(bestLabels(r*grayImg.cols + c)));
        }
    }

    imshow("Image", grayImg);
    imshow("Result", result);
    waitKey();

    return 0;
}

运行查询的代码:

//CREATE FILE
NSError *error;

NSLog(@"string to write:%@",printString);
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

if (ubiq == nil) {
    return;
}
NSURL *cloudURL =[[ubiq URLByAppendingPathComponent:@"Documents" isDirectory:true] URLByAppendingPathComponent:@"CogwindInventory.txt"];
[printString writeToURL:cloudURL atomically:YES encoding:NSUTF8StringEncoding error:&error];

执行阅读的代码:

self.backups = [[NSMutableArray alloc] init];
self.query = [[NSMetadataQuery alloc] init];
[self.query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred = [NSPredicate predicateWithFormat: @"%K like 'CogwindInventory.txt'", NSMetadataItemFSNameKey];
[self.query setPredicate:pred];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(queryDidFinishGatheringRestore:)
                                             name:NSMetadataQueryDidFinishGatheringNotification
                                           object:self.query];
[self.CloudActivityIndicator startAnimating];
[self.query startQuery];

- (void)queryDidFinishGatheringRestore:(NSNotification *)notification {
    NSMetadataQuery *query = [notification object];
    [query disableUpdates];
    [query stopQuery];

    [self loadDataRestore:query];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSMetadataQueryDidFinishGatheringNotification object:query];

    self.query = nil;
}


- (void)loadDataRestore:(NSMetadataQuery *)query {
    [self.backups removeAllObjects];

    for (NSMetadataItem *item in [query results]) {
        NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
        //[self.backups addObject:url.lastPathComponent];
        [self.backups addObject: url.filePathURL];
    }
    if (self.backups.count > 0) {
        [self retrievefile];
    }
    [self OperationComplete];
}

设备是OS 8.1.1和OS 8.3。 iCloud-文档在Xcode中启用,但不在iCloudKit中启用。

任何帮助表示赞赏。谢谢。

0 个答案:

没有答案