简体   繁体   English

iOS8扩展:在容器和扩展之间共享图像

[英]iOS8 extension : share images between container and extension

I'm making an iOS 8 extension. 我正在进行iOS 8扩展。 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. 这就是我要做的事情:用户从容器应用程序中的照片库中选择图像,这些图像将与扩展程序共享并供进一步使用。

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

Here's the coding: 这是编码:

  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
}

When I want to retrieve the images: 当我想要检索图像时:

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 would be the array of images I want to get. convertedArray将是我想要的图像数组。

Apparently, there are a lot of problems if I do it this way. 显然,如果我这样做会有很多问题。 For example, the two major issues: 例如,两个主要问题:

  • 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). 实际完成这个过程需要大约半分钟。如果我有一个包含大约20张图像的数组,我会得到“didRecieveMemoryWarning”大约3次(我使用iPad mini作为测试设备)。 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! 在弹出viewController后(这意味着它运行到我的存储代码的最后一行), 我刚刚保存到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. 我确信我的编码工作正常,这个问题是由内存不足引起的,因为如果阵列的图像少于15个,我可以完美地保存和检索它们。

  • 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. 当我想这样做时,我必须检索先前的数组并将新的图像数据添加到该数组中,然后将新数组保存到UserDefault中。 As mentioned before, saving an array into the UserDefault takes a lot of memory. 如前所述,将数组保存到UserDefault会占用大量内存。

So my questions are pretty straight foward and specific: 所以我的问题非常直接且具体:

  1. Are there any other ways to transfer images from one target to another? 有没有其他方法可以将图像从一个目标传输到另一个目标? In other words: How can I transfer images from the container app to the extension? 换句话说:如何将图像从容器应用程序传输到扩展程序?
  2. If not, are there any ways to solve the issue in my codes? 如果没有,有没有办法解决我的代码中的问题? Is this a proper way to do it? 这是一个正确的方法吗?

Those are all I want to ask, but if you could answer following questions for me also, it will be really nice: 这些都是我想问的问题,但如果你能为我回答以下问题,那将是非常好的:

  1. Why would I get more than one "didRecieveMemoryWarning" in one saving process? 为什么我会在一个保存过程中获得多个“didRecieveMemoryWarning”? When the system received memory warning, will it stop the action immediately? 当系统收到内存警告时,它会立即停止操作吗?
  2. (Just to make sure) Is that safe to use UIImagePNGRepresentation for all the images including PNG and JPG? (只是为了确保)对包括PNG和JPG在内的所有图像使用UIImagePNGRepresentation是否安全?

Thank you. 谢谢。

From Apple's Documentation on App Extension Programming 来自Apple的App扩展编程文档

Sharing Data with Your Containing App 与您的应用程序共享数据

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. 例如,您可能希望允许您的应用扩展程序及其包含的应用程序共享一组大量数据,例如预渲染资产。

..... .....

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. 使用Core Data,SQLite或Posix锁来帮助协调共享容器中的数据访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM