在 iPhone 中使用 UIImagePickerControllerSourceTypeCamera 捕获图像 10 或 11 次后收到内存警告
您好,每当我使用相机时,我都会收到内存警告。
错误是这样的:
"Receive memory warning..."
代码是:
-(void) getPhoto{
GameAppdelegate *appDelegate = (GameAppdelegate *)[[UIApplication sharedApplication]delegate];
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
///////////////////////////////////photolibrary//////////////////////////////
if([appDelegate.photoselection isEqualToString:@"User Pressed Button 1\n"])
{
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
if(appDelegate.sound == 1)
{
[classObj ButtonSound];
}
}
///////////////////////////////////Camera//////////////////////////////
else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 2\n"])
{
@try
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
@catch (NSException * e)
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT"
message:@"Please try again"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"ok", nil];
[av show];
}
if(appDelegate.sound == 1)
{
[classObj ButtonSound];
}
}
///////////////////////////////////Cancel//////////////////////////////
else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 3\n"])
{
if(appDelegate.sound == 1)
[classObj ButtonSound];
return;
}
[self presentModalViewController:picker animated:YES];
[picker release];
}
我该如何处理这个?拍照后请帮助我裁剪图像并保存在应用程序中,如下所示:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];
if(appDelegate.sound == 1)
{
[classObj ButtonSound];
}
[picker dismissModalViewControllerAnimated:YES];
imageView.image = image;
CGSize size = [imageView.image size];
CGRect cropRect = CGRectMake(0.0, 0.0, size.width, size.height);
NSValue *cropRectValue = [editingInfo objectForKey:@"UIImagePickerControllerCropRect"];
cropRect = [cropRectValue CGRectValue];
appDelegate.slectedimage = image;
imageView.hidden = YES;
if( [appDelegate.Name length] != 0 && max_att == 15)
{
btnNotDone.hidden = YES;
btnDone.enabled = YES;
}
//IMAGE SAVE IN DOCUMENTS//////
[UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
// Show the current contents of the documents folder
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
}
请帮助我。我想删除所有警告。
Hi I receive memory warning whenever I am using camera.
The error is like this:
"Receive memory warning..."
And the code is:
-(void) getPhoto{
GameAppdelegate *appDelegate = (GameAppdelegate *)[[UIApplication sharedApplication]delegate];
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
///////////////////////////////////photolibrary//////////////////////////////
if([appDelegate.photoselection isEqualToString:@"User Pressed Button 1\n"])
{
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
if(appDelegate.sound == 1)
{
[classObj ButtonSound];
}
}
///////////////////////////////////Camera//////////////////////////////
else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 2\n"])
{
@try
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
@catch (NSException * e)
{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"ALERT"
message:@"Please try again"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"ok", nil];
[av show];
}
if(appDelegate.sound == 1)
{
[classObj ButtonSound];
}
}
///////////////////////////////////Cancel//////////////////////////////
else if([appDelegate.photoselection isEqualToString:@"User Pressed Button 3\n"])
{
if(appDelegate.sound == 1)
[classObj ButtonSound];
return;
}
[self presentModalViewController:picker animated:YES];
[picker release];
}
How can i handle this?please help after taking picture i crop the image and save in application like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
iRolegameAppDelegate *appDelegate = (iRolegameAppDelegate *)[[UIApplication sharedApplication]delegate];
if(appDelegate.sound == 1)
{
[classObj ButtonSound];
}
[picker dismissModalViewControllerAnimated:YES];
imageView.image = image;
CGSize size = [imageView.image size];
CGRect cropRect = CGRectMake(0.0, 0.0, size.width, size.height);
NSValue *cropRectValue = [editingInfo objectForKey:@"UIImagePickerControllerCropRect"];
cropRect = [cropRectValue CGRectValue];
appDelegate.slectedimage = image;
imageView.hidden = YES;
if( [appDelegate.Name length] != 0 && max_att == 15)
{
btnNotDone.hidden = YES;
btnDone.enabled = YES;
}
//IMAGE SAVE IN DOCUMENTS//////
[UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
// Show the current contents of the documents folder
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
}
Please help me. I want to remove all warnings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在泄漏
UIImagePickerController
。创建时自动释放它或在dismissModalViewControllerAnimated
之后释放它。您可能仍然会收到内存警告,照片可能会很大,尤其是在 iPhone 4 上,并且在某个时刻,您的内存中有两个照片:
UIImage
和自动释放的 PNG。PS您似乎没有使用
size
和cropRect
所以您可以删除它们。You're leaking the
UIImagePickerController
. Autorelease it on creation or release it afterdismissModalViewControllerAnimated
.You may still get memory warnings, photos can be enormous, especially on the iPhone 4 and at a certain point you have two of them in memory: the
UIImage
and the autoreleased PNG.P.S. you don't seem to be using
size
andcropRect
so you could delete them.释放您的警报视图。一般来说,释放您分配的任何对象。如果您有保留属性,则为其分配一个自动释放对象。
当您收到内存警告时,您的视图控制器方法
- (void)didReceiveMemoryWarning
将被调用。在这里,您必须释放已缓存的所有不需要的对象。通常,这将是一些图像、堆栈中的视图等。还要检查模态视图控制器中的对象是否有适当的释放。
Release your alert view. In general release any object you alloc. In case you have a retain property then assign a auto release object to it.
When you get a memory warning your view controller method
- (void)didReceiveMemoryWarning
is called. Here you will have to release any unwanted objects which you have cached. Typically that would be some images, views in stack etc.Also check if you are having appropriate dealloc for objects in your modal view controller.
您是否正在实施
-imagePickerController:didFinishPickingMediaWithInfo:
?您实施的方法已被弃用。即使对于图像,您也应该使用其他方法。你用录制的视频做什么?顺便说一句,以下代码 -
应该是
现在更聪明的事情是禁用按钮 2,这将是
Are you implementing
-imagePickerController:didFinishPickingMediaWithInfo:
? The method you've implemented has been deprecated. You should use the other method even for images. What are you doing with the recorded videos?On a side note, the following code –
should be
Now smarter thing would be to disable button 2 which would be