iOS8扩展:在容器和扩展之间共享图像 [英] iOS8 extension : share images between container and extension

查看:75
本文介绍了iOS8扩展:在容器和扩展之间共享图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作iOS 8扩展程序。以下是我要做的事情:用户从容器应用程序中的照片库中选择图像,这些图像将与扩展程序共享并供进一步使用。

I'm making an iOS 8 extension. Here's what I'm trying to do: Users select images from the photo library in the container app, and these images will be shared with the extension and for the further use.

现在我正在这样做(如果您不想阅读此部分,请跳过下面的内容阅读实际代码):使用App Group和NSUserDefaults共享数据。将UIImage转换为NSData然后将所有图像保存在NSArray中,然后将数组保存到NSDictionary中(我有很多数组,这就是我组织它们的方式 - 所以我必须将它们保存到字典中),最后将字典保存到用户默认。

Right now I'm doing it in this way (If you don't want to read this part, please skip below to read the actual codes): Use App Group and NSUserDefaults to share datas. Convert UIImage into NSData and then save all the images in a NSArray, then save the array into a NSDictionary (I have many arrays and this is the way I organize them - so I have to save them into dictionary), finally save the dictionary into user default.

以下是编码:

  NSArray *imageArray = ... 
 //this array contains all the images.
 //photoDataArray is a NSMutableArray;
  photoDataArray = [[NSMutableArray alloc]init];
for (UIImage *images in imageArray) {
    [photoDataArray addObject:UIImagePNGRepresentation(images)];
}
    NSThread * creationThread = [[NSThread alloc] initWithTarget:self selector:@selector(handleData) object:nil];
    [creationThread start];
     -(void)handleData{
        NSDictionary *dic = [[NSDictionary alloc]init];
        [dic SetObject:photoDataArray forKey:@"testImageArray"];
        NSUserDefaults *   def = [[NSUserDefaults alloc] initWithSuiteName:@"group.myCompany.myApp"];
        [def setObject:dic forKey:@"dataDic"];
//done with saving data
        [self.navigationController popViewControllerAnimated:YES];
//Navigation
}

当我想要检索图像时:

NSUserDefaults *   def = [[NSUserDefaults alloc] initWithSuiteName:@"group.myCompany.myApp"];
NSDictionary *dic = [def ObjectForKey:@"dataDic"];

  NSArray *dataArray = [dic objectForKey:@"testImageArray"];
    NSMutableArray *convertedArray = [[NSMutableArray alloc] init];
    for (NSData *imageData in dataArray) {
        [convertedArray addObject:[UIImage imageWithData:imageData]];
    }

convertedArray将是我想要的图像数组。

convertedArray would be the array of images I want to get.

显然,如果我这样做会有很多问题。
例如,两个主要问题:

Apparently, there are a lot of problems if I do it this way. For example, the two major issues:


  • 这样做会占用大量资源,包括内存。实际完成这个过程需要大约半分钟。如果我有一个包含大约20张图像的数组,我会得到didRecieveMemoryWarning大约3次(我使用iPad mini作为测试设备)。 有时数据无法正确保存。弹出viewController后(这意味着它运行到我的存储代码的最后一行),我刚刚保存到UserDefault中的数组为nil!我确信我的编码都能正常工作,这个问题是由内存不足造成的,因为如果阵列的图像少于15张,我可以完美地保存和检索它们。

  • Doing this takes a lot of resources including memory. It takes about half minute to actually finish the process.If I have a array with about 20 images, I'll get "didRecieveMemoryWarning" about 3 times (I'm using a iPad mini as a test device). Sometimes the datas are not saved correctly. After the viewController is popped out(which means it runs to the last line of my storing code), I get nil for the array I just saved into the UserDefault! I'm sure my coding all worked normal, and this issue is caused by low memory because if the array has less than 15 images, I can save and retrieve them perfectly.

很难将新图像保存到以前保存的数组中。当我想这样做时,我必须检索先前的数组并将新的图像数据添加到该数组中,然后将新数组保存到UserDefault中。如前所述,将数组保存到UserDefault会占用大量内存。

It's hard to save new images into a previously saved array. When I want to do that, I have to retrieve the previous array and add new image datas into that array, and then save the new array into the UserDefault. As mentioned before, saving an array into the UserDefault takes a lot of memory.

所以我的问题非常直接具体:

So my questions are pretty straight foward and specific:


  1. 有没有其他方法可以将图像从一个目标传输到另一个目标?换句话说:如何将图像从容器应用程序传输到扩展程序?

  2. 如果没有,是否有任何方法可以解决我的代码中的问题?这是一个正确的方法吗?

这些都是我想问的问题,但如果你能回答以下问题我也是,这将是非常好的:

Those are all I want to ask, but if you could answer following questions for me also, it will be really nice:


  1. 为什么我会在一个保存过程中获得多个didRecieveMemoryWarning?当系统收到内存警告时,它会立即停止操作吗?

  2. (只是为了确保)对包括PNG和JPG在内的所有图像使用UIImagePNGRepresentation是否安全?

谢谢。

推荐答案

来自 Apple关于App Extension编程的文档

与您的应用程序共享数据

应用程序扩展的安全域及其包含应用程序是不同的,即使扩展包嵌套在包含应用程序的包中。默认情况下,您的扩展程序及其包含的应用程序无法直接访问彼此的容器。

The security domains for an app extension and its containing app are distinct, even though the extension bundle is nested within the containing app’s bundle. By default, your extension and its containing app have no direct access to each other’s containers.

但是,您可以启用数据共享。例如,您可能希望允许您的应用扩展程序及其包含的应用共享一组大量数据,例如预呈现资产。

You can, however, enable data sharing. For example, you might want to allow your app extension and its containing app to share a single large set of data, such as prerendered assets.

.....

当您设置共享容器时,包含应用程序以及您允许参与数据共享的每个包含应用程序扩展名 - 具有对共享容器的读写权限。为避免数据损坏,必须同步数据访问。使用Core Data,SQLite或Posix锁来帮助协调共享容器中的数据访问。

When you set up a shared container, the containing app—and each contained app extension that you allow to participate in data sharing—have read and write access to the shared container. To avoid data corruption, you must synchronize data accesses. Use Core Data, SQLite, or Posix locks to help coordinate data access in a shared container.

这篇关于iOS8扩展:在容器和扩展之间共享图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆