当我在 iPhone 上使用相机时收到内存警告。我也在使用 ARC。
当您拍照并按相机视图控制器上的“使用照片”按钮时,我会收到内存警告。目的是一旦按下“使用照片”按钮,它就会更改 ImageView 的内容。
我认为内存问题可能是由于捕获的图像是全屏的,并且ImageView是250h 250w。但我尝试缩小相机拍摄的图像的大小,然后将其分配给 ImageView。然而,即使我将其大小调整为 100 x 100,这仍然不起作用。
其次,我没有将相机拍摄的照片分配给ImageView,但它仍然有内存警告。
我在这里查看了其他答案并尝试了上面的两个答案,但它仍然存在。我将在下面展示我的代码。这会影响我向应用商店提交内容吗?当然,如果这种情况很常见,那么它是一个错误还是有解决方法?如果有人可以查看提供的代码并发现错误或建议如何处理此内存警告,那就太好了?
除了这个内存警告之外,我的应用程序已完成 95% 以上,因此已接近提交时间。
My code:
- (IBAction)takePhoto:(id)sender {
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing=NO;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:self.imagePicker animated:YES completion:NULL];
}
else{
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.imagePicker animated:YES completion:NULL];
}
}
- (IBAction)choosePhoto:(id)sender {
self.imagePicker2 = [[UIImagePickerController alloc] init];
self.imagePicker2.delegate = self;
self.imagePicker2.allowsEditing=NO;
[self.imagePicker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:self.imagePicker2 animated:YES completion:NULL];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
CGRect rect = CGRectMake(0,0,100,100);
UIGraphicsBeginImageContext( rect.size );
[self.image drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.snapImage setImage:picture1];
[self.uploadImageBtn setHidden:NO];
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}
我没有找到好的解决方案,但我不会将原始图像存储在属性中,因为原始图像占用大约 30 MB 的内存。所以而不是:
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
我把它改为:
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
这样,图像不再使用时就会被销毁。注意:我已经在 iPhone 4 系列和 5 上测试了这种新方法。内存警告仅出现在 4 系列上,而不是 5 系列上。
从网络上看,已经有许多关于相机和 iOS7 的错误报告提交给了 Apple。例如,当你启动相机时,它会不定期地给出黑色预览 - 这与 iOS7 相关,尤其是 iPhone 4 系列而不是 5。这可能是处理器能力的差异 - 但我不确定。我的应用程序已获得应用程序商店批准,因此内存警告不会成为问题 -
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)