我有一个更大的应用程序,应该能够分享多个图像。我使用UIActivityViewController和UIActivityItemProvider实现了这一点,以便异步使用这些项(这样,我一次只需要准备一个映像,而不必一次将它们全部填满内存来共享它们)。
我受到了苹果空投的例子的“启发”:AirdropSample download
然而,当使用我的应用程序共享9张图像(到相机卷==“保存9张图像”)时,只有4到7张图像最终出现在相机卷中,没有任何错误消息。如果我一遍又一遍地重复,有时我会得到5张图片或6张看似随机的图片。
我不能在这里张贴我的应用程序,但我修改了上面的样本,它也将随机“失败”,将所有图像发送到camera Roll…
如果您下载上述示例并用这些文件替换4个文件,则会显示问题:
APLAsyncImageViewController.h:
#import <UIKit/UIKit.h>
#import "APLAsyncImageActivityItemProvider.h"
@interface APLAsyncImageViewController : UIViewController
@endAPLAsyncImageViewController.m:
#import "APLAsyncImageViewController.h"
#import "APLProgressAlertViewController.h"
NSString * const kProgressAlertViewControllerIdentifier = @"APLProgressAlertViewController";
@interface APLAsyncImageViewController ()
@property (strong, nonatomic) UIWindow *alertWindow;
@property (strong, nonatomic) APLProgressAlertViewController *alertViewController;
@property (strong, nonatomic) UIPopoverController *activityPopover;
@property (weak, nonatomic) IBOutlet UIButton *shareImageButton;
- (IBAction)openActivitySheet:(id)sender;
@end
@implementation APLAsyncImageViewController
- (IBAction)openActivitySheet:(id)sender
{
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
for( int i = 0; i < 9;i++)
{
APLAsyncImageActivityItemProvider *aiImageItemProvider = [[APLAsyncImageActivityItemProvider alloc] init];
[itemArray addObject: aiImageItemProvider];
}
//Create an activity view controller with the activity provider item. UIActivityItemProvider (AsyncImageActivityItemProvider's superclass) conforms to the UIActivityItemSource protocol
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:itemArray applicationActivities:nil];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//iPhone, present activity view controller as is
[self presentViewController:activityViewController animated:YES completion:nil];
}
else
{
//iPad, present the view controller inside a popover
if (![self.activityPopover isPopoverVisible]) {
self.activityPopover = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[self.activityPopover presentPopoverFromRect:[self.shareImageButton frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
//Dismiss if the button is tapped while pop over is visible
[self.activityPopover dismissPopoverAnimated:YES];
}
}
}
@endAPLAsyncImageActivityItemProvider.h:
#import <UIKit/UIKit.h>
@interface APLAsyncImageActivityItemProvider : UIActivityItemProvider
@endAPLAsyncImageActivityItemProvider.m:
#import "APLAsyncImageActivityItemProvider.h"
#import "UIImage+Resize.h"
@implementation APLAsyncImageActivityItemProvider
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
return [[UIImage alloc] init];
}
- (id)item
{
UIImage *image = [UIImage imageNamed:@"Flower.png"];
//CGSize imageSize;
//imageSize.height = 1000;
//imageSize.width = 1000;
//image = [UIImage imageWithImage:image scaledToFitToSize:imageSize];
return image;
}
- (UIImage *)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(NSString *)activityType suggestedSize:(CGSize)size
{
//The filtered image is the image to display on the other side.
return [[UIImage alloc] init];
}
@end如果您像这样执行示例(使用菜单项“预处理后发送图像”,按下“共享”按钮),它将经常或大多数情况下无法将所有9张图像传送到相机胶卷。
如果您取消注释"APLAsyncImageActivityItemProvider.m“中基本上只是缩放输出图像的4行,那么它将始终工作。
你能告诉我为什么吗?我觉得如果我知道这个谜语的答案,我也可以修复我的应用程序。
谢谢,
Nils
发布于 2015-04-20 18:09:25
这个问题的原因很简单。
可供应用程序使用的写入器线程数量有限,因此同时写入太多图像可能会由于“写入忙”错误而失败。您可以通过尝试使用UIImageWriteToSavedPhotosAlbum (或相应的资源/照片框架副本)手动写入这些图像来验证这一点。因此,调整图像大小只是隐藏了真正的问题,因为写入较小的图像需要更少的时间。
这个问题可以通过限制写入器的数量和/或在出错的情况下重试来解决。还要注意的是,-UIActivityItemProvider item是一个阻塞调用,但是没有同步的方法可以开箱即用地写入库。这可以使用dispatch_semaphore_wait、NSCondition等来处理,或者,如果您使用的是ReactiveCocoa,则只需调用waitUntilCompleted。
发布于 2014-09-17 01:09:02
我会说,在保存到相机胶卷之前,创建的[UIImage imageWithImage:image scaledToFitToSize:imageSize]图像是GC,但如果没有代码,我不知道该方法中发生了什么。该方法不是一个普通的UIImage方法,也许是一个类别?
编辑
将添加到项目数组中的for循环代码替换为以下代码,然后取消对调整大小的行的注释,看看是否有效。
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
for( int i = 0; i < 9;i++)
{
UIImage *aiImage = [[[APLAsyncImageActivityItemProvider alloc] init] image];
[itemArray addObject:aiImage];
}https://stackoverflow.com/questions/25874568
复制相似问题