UIImagePickerController Low Memory crash at ios7 on iPad Mini - iphone

My app works fine on all the devices and ios versions but when it comes to ipad mini, on iOS7 when i alloc:init the UIImagePickerController and display. After getting image the app suddenly give low memory warning and crash. Here is my code of capturing image.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePickerController;
if ([UIUtilityClass isCurrentVersionIsIOS7OrGreater]) {
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
[imagePickerController setDelegate:self];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage,nil];
imagePickerController.allowsEditing=NO;
CGFloat scaleFactor=1.3f;
switch ([UIApplication sharedApplication].statusBarOrientation) {
case UIInterfaceOrientationLandscapeLeft:
imagePickerController.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * 90 / 180.0), scaleFactor, scaleFactor);
break;
case UIInterfaceOrientationLandscapeRight:
imagePickerController.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * -90 / 180.0), scaleFactor, scaleFactor);
break;
case UIInterfaceOrientationPortraitUpsideDown:
imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(M_PI * 180 / 180.0);
break;
default:
break;
}
}
else
{
imagePickerController = [[NonRotatingUIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeImage,nil];
imagePickerController.allowsEditing = YES;
}
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
popoverController.delegate=self;
[popoverController presentPopoverFromRect:CGRectMake(626,142,120,135) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[imagePickerController release];
newMedia = YES;
}
After that i capture the image using following code.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[popoverController dismissPopoverAnimated:true];
[popoverController.delegate popoverControllerDidDismissPopover:popoverController];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
int orientation=image.imageOrientation;
image=[image imageToFitSize:PIC_SIZE method:MGImageResizeCrop];
switch (orientation) {
case UIImageOrientationUp:
// do nothing
break;
case UIImageOrientationDown:
image=[image imageRotatedByDegrees:180.0];
break;
case UIImageOrientationLeft:
image=[image imageRotatedByDegrees:90.0];
image=[image imageRotatedByDegrees:180];
break;
//2
case UIImageOrientationRight:
image=[image imageRotatedByDegrees:-90.0];
image=[image imageRotatedByDegrees:180];
break;
default:
break;
}
}

Did you test it without the release call to the imagePickerController? If you are using ARC, I don't think you need to do this.

Related

dismissModelViewControllerAnimated not working for iPhone 5

So in my iPhone 4 device after I pick a image I want the image picker popover to go away. This works in iPhone 4 but the following code doesn't work for the iPhone 5.
- (void) loadImage:(UIImage*) image {
float w = image.size.width;
float h = image.size.height;
float maxw = scrollView.frame.size.width;
float maxh = scrollView.frame.size.height;
float wratio = maxw / w;
float hratio = maxh / h;
float ratio = wratio < hratio ? wratio : hratio;
ratio = ratio < 1 ? ratio : 1;
int adjW = (int) (w * ratio);
int adjH = (int) (h * ratio);
UIGraphicsBeginImageContext(CGSizeMake(adjW, adjH));
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0, 0, adjW, adjH)];
CGImageRef scaledImage = CGBitmapContextCreateImage(context);
UIGraphicsEndImageContext();
[appDelegate.model setCurrentImage:[UIImage imageWithCGImage: scaledImage]];
[self clear];
calcButton.enabled = YES;
trashButton.enabled = YES;
scribbleControls.enabled = YES;
[appDelegate.mergeViewCtlr setFirst];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self loadImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
if ([popoverController isPopoverVisible]) {
// called for iPad
[popoverController dismissPopoverAnimated:YES];
}
}
else {
// called for iPhone and tried each of the next 3 lines individually
[self dismissModalViewControllerAnimated:YES]; <== NOT WORKING
[self dismissViewControllerAnimated:YES completion:nil]; <== ALSO NOT WORKING
[picker dismissViewControllerAnimated:YES completion:nil]; <== ALSO NOT WORKING
}
[picker release];
}
I also noticed that it said dimissModelViewControllerAnimated has been deprecated and that instead I should use:
Use dismissViewControllerAnimated:completion: instead. But how would I use this? Thanks
Here is the code that presents the model view:
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: { //photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
}
else {
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
popoverController.delegate = self;
[popoverController presentPopoverFromRect:CGRectMake( 250, -50, 320, 480 )
inView:[self view]
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
}
else { // for iPhone
[self presentModalViewController:imagePicker animated:TRUE];
}
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Photo library is empty or unavailable" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
break;
}
case 1: //camera
dismissModalViewControllerAnimated:: was deprecated in iOS6. Use dismissViewControllerAnimated:completion: instead.
[self dismissViewControllerAnimated:YES completion:nil];
presentModalViewController, and dismissModalViewController depracated in iOS6
Try this,
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];
and use
[self presentViewController:self.picker animated:YES completion:nil];
You can also use respondsToSelector to make the cases for iOS 5 and ios6.

Taking too much time in loading news from rss feed

I am working in a news app. I have six different news. I am calling the rss feeds from viewwillappear() method. I have run a switch case with the selectedIndex. I included lazy loading but still it takes a huge time to load news from rss.
Any probabale solutions?
Thanks in advance
(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
noNewsLabel.hidden=YES;
bdNewsParser=[[[BDNewsParser alloc] init]retain];
if (loadingFlag==1)
{
ImgesName=[[NSMutableArray alloc] init];
dicImages_msg = [[NSMutableDictionary alloc] init];
}
if (loadingFlag!=2)
{
NSLog(#"newsTypeInd:%#",newsTypeInd);
[newsCat setText:[newsTypeInd objectAtIndex:selectedIndex]];
[newsTitleArray removeAllObjects];
[newsDescriptionArray removeAllObjects];
[newsLinkArray removeAllObjects];
[newsPublicationDate removeAllObjects];
[newsImageLinkArray removeAllObjects];
[newsSourceArray removeAllObjects];
switch(selectedIndex)
{
case 0:
[bdNewsParser loadXMLByURL:#"http://amarhost.info/fuad/newsainment/itnews.php"];
break;
case 1:
[bdNewsParser loadXMLByURL:#"http://amarhost.info/fuad/newsainment/internationalnews.php"];
break;
case 2:
// [bdNewsParser loadXMLByURL:#"http://amarhost.info/fuad/newsainment/bangladeshnews.php"];
[self showAlert];
break;
case 3:
//[bdNewsParser loadXMLByURL:#"http://amarhost.info/fuad/newsainment/businessnews.php"];
[self showAlert];
break;
case 4:
[bdNewsParser loadXMLByURL:#"http://amarhost.info/fuad/newsainment/entertainmentnews.php"];
break;
case 5:
[bdNewsParser loadXMLByURL:#"http://amarhost.info/fuad/newsainment/sportnews.php"];
break;
default:
break;
}
newsTitleArray=bdNewsParser.titleArray;
newsDescriptionArray=bdNewsParser.descriptionArray;
newsLinkArray=bdNewsParser.linkArray;
newsPublicationDate=bdNewsParser.publicationDateArray;
newsImageLinkArray=bdNewsParser.newsImageArray;
newsSourceArray=bdNewsParser.newsSourceArray;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// [self filterArrayGroupWise:selectedIndex];
if ([newsTitleArray count]==0) {
NSLog(#"Hide contrrol");
[self hideControls];
noNewsLabel.hidden=NO;
}
else {
[self showControls];
noNewsLabel.hidden=YES;
[self setupScrollview];
ImgesName = newsImageLinkArray;
[self reloadScrolview];
}
savedTitleArr=[userDefault readArrayFromUserDefaults:#"FavoriteTitle"];
if (savedTitleArr)
{
if ([savedTitleArr containsObject:[newsTitleArray objectAtIndex:pageNo]])
{
[saveButton setEnabled:NO];
}
else
{
[saveButton setEnabled:YES];
}
}
else
{
[saveButton setEnabled:YES];
}
}
indicatorAnimatingFlag=0;
[activityView stopAnimating];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
pageNo=0;
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * 0;
frame.origin.y = 0;
[scrollView setBounds:frame];
[bdNewsParser release];
index=0;
selectedIndex=0;
}
You said "Lazy Loading", but where? It seems to me that you make the call and then you try to access its value:
case 0:
[bdNewsParser loadXMLByURL:#"http://amarhost.info/fuad/newsainment/itnews.php"];
break;
And then:
newsTitleArray=bdNewsParser.titleArray;
newsDescriptionArray=bdNewsParser.descriptionArray;
newsLinkArray=bdNewsParser.linkArray;
newsPublicationDate=bdNewsParser.publicationDateArray;
newsImageLinkArray=bdNewsParser.newsImageArray;
newsSourceArray=bdNewsParser.newsSourceArray;

Black screen appears, randomly after taking an image and tapping 'USE' in the UIImagePickerController

I have an app that's almost done and now while it was under QA, the QA engineer came across a random issue in which a black screen appears after tapping on USE in UIImagePickerController view. Further in didFinishPickingMediaWithInfo the image is being saved for future refrencing and is being shown in an UIImageView, I am also using camera overlay for adding a button to the UIImagePickerView and the max memory usage of the app doen't goes beyond 13 MB. The issue is not arising when switching to Camera roll mode from capture mode. Also in the same view iADs are being presented. Underneath is the code, to which the issue may concern, also images are being attached to get an idea of the issue. Any help would be greatly appreciated.
Thanks in advance.
(void) launchCamera
{
// Set up the camera\
CustomCameraView *cameraController = [[CustomCameraView alloc] init];
cameraController.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraController.delegate = self;
cameraController.showsCameraControls = YES;
cameraController.navigationBarHidden = YES;
cameraController.toolbarHidden = YES;
// overlay on top of camera lens view
UIButton *cameraRoll = [[UIButton alloc] initWithFrame:CGRectMake(15, 380, 40, 40)];
[cameraRoll setAlpha:0.0f];
[cameraRoll setBackgroundImage:[UIImage imageNamed:#"CamerRoll_New"] forState:UIControlStateNormal];
[cameraRoll addTarget:self action:#selector(switchToCameraRoll) forControlEvents:UIControlEventTouchUpInside];
cameraController.cameraOverlayView = cameraRoll;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:1.5f];
cameraRoll.alpha = 1.0f;
[UIView commitAnimations];
[self presentModalViewController:cameraController animated:YES];
}
-(void)switchToCameraRoll
{
DLog(#"Camera Roll");
[self dismissViewControllerAnimated:NO completion:^(void){ [self photoSourcePhotoAlbum];}];
}
-(void) photoSourcePhotoAlbum
{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.view addSubview:progressView];
timer = [NSTimer scheduledTimerWithTimeInterval:.017 target:self selector:#selector(progressChange) userInfo:nil repeats:YES];
[picker dismissModalViewControllerAnimated:YES];
[self performSelectorInBackground:#selector(saveImage:) withObject:info];
[self performSelector:#selector(navigateToEditView) withObject:nil afterDelay:2.1];
}
-(void)navigateToEditView
{
[self performSegueWithIdentifier:#"presentRDetailModalViewController" sender:self];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum)
{
[picker dismissViewControllerAnimated:NO completion:^(void){ [self photoSourceCamera];}];
}
else
{
[picker dismissModalViewControllerAnimated:YES];
}
}
-(void)saveImage:(NSDictionary *)info
{
NSString *imageName = [[[DataStaging dataStaging] getGUID] stringByAppendingString:#".jpg"];
rImageName = imageName;
UIImage *rImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//Compress image
[[FileOperations fileOperations] PersistData:UIImageJPEGRepresentation(rImage, 0.4) name:imageName];
DLog(#"%#",[[FileOperations fileOperations] fetchPath:imageName]);
//Actual image taken
[[self rImageView] setImage:[info objectForKey:#"UIImagePickerControllerOriginalImage"]];
if ([rImageName length] == 0)
{
[[self rDetails] setHidden:YES];
[[self trashRImage] setHidden:YES];
}
else
{
[[self rDetails] setHidden:NO];
[[self trashRImage] setHidden:NO];
}
[progressView removeFromSuperview];
}
}
Make sure you don't call presentModalViewController:animated: twice before dismissModalViewControllerAnimated:
It helped in my case.
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType
{
if (_imagePickerController!=nil || _imagePopoverController!=nil) {
return;
}
_imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
_imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
_imagePickerController.sourceType = sourceType;
_imagePickerController.delegate = self;
switch (sourceType) {
case UIImagePickerControllerSourceTypeCamera:
_imagePickerController.showsCameraControls = YES;
[self presentViewController:_imagePickerController animated:YES completion:nil];
break;
default:
_imagePopoverController = [[UIPopoverController alloc] initWithContentViewController:_imagePickerController];
_imagePopoverController.delegate = self;
CGRect rect = [[UIScreen mainScreen] bounds];
rect.origin.y = rect.size.height - 70.0;
rect.size.height -= rect.origin.y;
[_imagePopoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
break;
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[_imagePopoverController dismissPopoverAnimated:YES];
_imagePopoverController = nil;
[_imagePickerController dismissViewControllerAnimated:YES completion:nil];
_imagePickerController = nil;
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImageWriteToSavedPhotosAlbum([info objectForKey:UIImagePickerControllerOriginalImage],nil,nil,nil);
_currentImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[_imagePopoverController dismissPopoverAnimated:YES];
_imagePopoverController = nil;
[_imagePickerController dismissViewControllerAnimated:YES completion:nil];
_imagePickerController = nil;
}

UITextView & UITextField get refreshed when pick image from camera from iPhone device

Facing some weird moment of my career. I have some uitextfield's and one uitextview in my UIViewController....everything works fine on simulator and iPhone 4 iOS v 4.3.5. But in iPhone 4 iOS v 4.0.1 sometimes I get whole view refreshed. That meant sometimes my uitextfields & uitextview lost text after picking image from camera.....
for solving this my attempt was when view disappear I saved values in some array..... and when view did appear i show array values to textboxes.....its works fine with simulator too. but this time my apps get stucked on iPhone 4 iOS v 4.0.1.
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"title : %#",actionSheet.title);
if (![actionSheet.title isEqualToString:#"Success"] && ![actionSheet.title isEqualToString:#"Error"] && ![actionSheet.title isEqualToString:#"خطأ"]) {
if(buttonIndex == 1) {
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
} else {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController: picker animated:YES];
[picker release];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
float actualHeight = img.size.height;
float actualWidth = img.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = 320.0/480.0;
if(imgRatio!=maxRatio){
if(imgRatio < maxRatio){
imgRatio = 480.0 / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = 480.0;
}
else{
imgRatio = 320.0 / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = 320.0;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[img drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(imageCount == 1){
self.firstImage = image;
self.previewImage1.image =firstImage;
}else if(imageCount == 2){
self.secondImage = image;
self.previewImage2.image =secondImage;
}else if(imageCount == 3){
self.thirdImage = image;
self.previewImage3.image =thirdImage;
}else if(imageCount == 4){
self.fourthImage = image;
self.previewImage4.image =fourthImage;
}
[myImageNames replaceObjectAtIndex:imageCount-1 withObject:#"Assigned"];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
Solved..Deleted app from device. then restarted device and install build again....
Now no problem with uitextfield refresh on picking image from camera..
chill guys....:-)

iPhone code forUIImageViewControllerPickerSourceTypeCamera

- (IBAction)pickAndDecode:(id) sender {
UIImagePickerControllerSourceType sourceType;
int i = [sender tag];
switch (i) {
case 0: sourceType = UIImagePickerControllerSourceTypeCamera; break;
case 1: sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; break;
case 2: sourceType = UIImagePickerControllerSourceTypePhotoLibrary; break;
default: sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self pickAndDecodeFromSource:sourceType];
}
- (void) updateToolbar {
self.cameraBarItem.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
self.savedPhotosBarItem.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
self.libraryBarItem.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
self.archiveBarItem.enabled = true;
self.actionBarItem.enabled = (self.result != nil) && ([self.result actions] != nil) && ([self.result actions].count > 0);
}
- (void)pickAndDecodeFromSource:(UIImagePickerControllerSourceType) sourceType {
[self reset];
// Create the Image Picker
if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.sourceType = sourceType;
picker.delegate = self;
picker.allowsImageEditing = YES; // [[NSUserDefaults standardUserDefaults] boolForKey:#"allowEditing"];
// Picker is displayed asynchronously.
[self presentModalViewController:picker animated:YES];
} else {
NSLog(#"Attempted to pick an image with illegal source type '%d'", sourceType);
}
}
Where I Put this line in my above codes;
[picker setShowsCameraControls:FALSE];
please help me so that i can change the real view of iPhone camera according to my view which i have designed
Did you try to put it where you create and setup your image picker, i.e. in pickAndDecodeFromSource method?