【发布时间】:2016-04-02 08:45:30
【问题描述】:
在我的应用程序中,我从相机拍摄图像并将它们保存在 imageView 中。我的代码在 iPad、iPhone 5/6 上运行良好,但在 iPhone 4s iOS 9 上运行良好。此外,它在 iPhone 4s 上发出内存警告。
当我在关闭 imagePicker 后调试 imageview 中更改的图像时。配置文件视图的默认图像仍然存在,但调试器显示图像已更改。
像这样初始化选择器:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [UIImagePickerController new];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
UIPopoverController* popOverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[popOverController presentPopoverFromRect:_imgProfile.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}];
}else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self presentViewController:imagePicker animated:YES completion:nil];
}];
}
}
当用户拍摄图像并调用委托时,我使用以下代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
_imgProfile.image = [global scaleAndRotateImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
}];
[picker dismissViewControllerAnimated:YES completion:nil];
}
我还尝试将选择器图像保存在变量中以节省内存,如下所示:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
_imgProfile.image = img;
}];
[picker dismissViewControllerAnimated:YES completion:nil];
}
我也使用 dispatch_async main 但在 iPhone 4s 中它不起作用。
【问题讨论】:
-
您是否有理由到处使用 NSOperationQueue?我的意思是你不能没有它吗?我不认为将它添加到本地 img 变量会有任何不同。
-
我在堆栈溢出的某个地方发现使用 NSOperation 解决了这个问题......但这不是这个问题的解决方案
-
你能不使用 NSOperationQueue 试试这个吗?然后会发生什么?
-
NSOperation 保证该方法何时被调用。
-
即使在删除 NSOperationQueue 后这也不起作用
标签: ios objective-c iphone ios9