2012-07-18 1 views
0
//This method Launches the picker to take the picture from camera. 
-(IBAction)takeyouphoto:(id)sender 
{ 
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
     { 
      // Create image picker controller 

      UIImagePickerController *imagePicker2 = [[UIImagePickerController alloc] init]; 

      // Set source to the camera 
      imagePicker2.sourceType = UIImagePickerControllerSourceTypeCamera; 

      // Delegate is self 
      imagePicker2.delegate = self; 
      // Allow editing of image ? 
      imagePicker2.allowsEditing= NO; 

      // Show image picker 
      [self presentModalViewController:imagePicker2 animated:YES]; 

     } 
} 
//This is ImagePicker Delegate method. 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
@try { 
     NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
     { 
      UIImage *resultimage=nil; 
      //I am using iOS5, so we can not use NSAutoreleasePool 
      resultimage=[info objectForKey:UIImagePickerControllerOriginalImage] ; 

      //This Launches the HUD (Activity Indicator) because ImagePicker ususally takes 5 
      //seconds to launch image. 
      [self showHUD:resultimage]; 
     } 
    } 
     [picker dismissModalViewControllerAnimated:YES]; 
} 
-(void)showHUD:(UIImage *)resultimage 
{ 
    [[Singleton sharedmysingleton] stoptimer]; 
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view]; 
    [self.navigationController.view addSubview:HUD]; 

    HUD.delegate = self; 
    HUD.labelText = @"Loading Image"; 
    //[email protected]"Loading"; 
    //Below call on showWhileExecuting of the MBProgressHuD class has its own NSAutoreleasePool 
    //Defined in MGProgressHUD class. it also runs the method showimageincell; in separate 
    //thread. 

    [HUD showWhileExecuting:@selector(showimagesincell:) onTarget:self withObject:resultimage animated:YES]; 

} 
-(void)showimagesincell:(UIImage *)image 
{ 

    appDelegate.tabbarcontroller.tabBar.userInteractionEnabled=NO; 
    NSError *error; 

    UIImage *resultImage=[self scale:image toSize:image.size]; 

    //UIImage *resultImage = [[UIImage alloc] initWithCGImage:imgRefCrop scale:1.0 orientation:resultimage.imageOrientation]; 

    //resultimage.imageOrientation 
    NSData *imagedata=UIImageJPEGRepresentation(resultImage, 0.7);//(resultImage); 



    UIImage *smallimage=[self scale:image toSize:CGSizeMake(100, 100)]; 
    NSData *smallimagedata=UIImageJPEGRepresentation(smallimage, 0.7); 

    /* NSString *imagetypeid=[Fetchsavefromcoredata getImagenameandImageidfromdatabase:@"Mobile_ImageType" attributename:@"imageType" predicate:imagetypetxtfield.text]; 

    //write image to document directory 
    NSString *localImagedir=[photodirpath stringByAppendingPathComponent:selectedvinnumber]; 
    NSString *datetime=[Singleton imagedateandtime]; 
    NSString *imagename=[NSString stringWithFormat:@"%@_%@.png",imagetypeid,datetime]; 
    NSString *localImagePath=[localImagedir stringByAppendingPathComponent:imagename]; 
    [imagedata writeToFile:localImagePath atomically:YES];*/ 

    [self performSelectorOnMainThread:@selector(updatetableview) withObject:nil waitUntilDone:NO]; 
} 

-(void)updatetableview 
{ 
    [[Singleton sharedmysingleton] starttimer]; 
    [self viewWillAppear:YES]; 
} 


-(UIImage *)scale:(UIImage *)image toSize:(CGSize)size 
{ 
    UIGraphicsBeginImageContext(size); 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return scaledImage; 
} 


//Above is all my code, I have tried to find it on diffrent forums but I have not fixed it yet. 
//Any help will be appreciated. Thanks in advance 
+0

구현 후? 나는 상대적으로 당신이 기억 문제를 가지고 있다고 확신하기 때문에. – Jake

+0

ARC를 사용하고 있습니까? 피커 위임자를 사용하고 있지 않습니까? 보고있는 오류 메시지를 게시 할 수 있습니까? –

+0

예, ARC를 사용하고 있습니다. 콘솔의 디버거에 오류가 표시되지 않습니다. 앱을 종료하고 때로는 흰색 화면을 종료합니다. – Rahul

답변

0

UIIMagePickerController 대리자 메서드에서 반환 한 이미지를 @autoreleasepool (iOS5의 경우)에 유지하면 흰색 화면 문제가 해결됩니다. 문제를 해결했지만 ARC 코드에서 NSAutoreleasePool을 사용할 수 없습니다. 여기

didFinishPickingMediaWithInfo 코드의 라인 : 위임 방법 UIImagePickerController를 위임 방법 아래

UIImage *resultimage=nil; 
@autoreleasepool 
{ 
      //I am using iOS5, so we can not use NSAutoreleasePool 
     resultimage=[info objectForKey:UIImagePickerControllerOriginalImage] ; 

} 

@autoreleasepool는 ARC를 사용하십니까

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
@try { 
     NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) 
     { 
      UIImage *resultimage=nil; 
      @autoreleasepool 
      { 
       //I am using iOS5, so we can not use NSAutoreleasePool 
       resultimage=[info objectForKey:UIImagePickerControllerOriginalImage] ; 
      } 

      //This Launches the HUD (Activity Indicator) because ImagePicker ususally takes 5 
      //seconds to launch image. 
      [self showHUD:resultimage]; 
     } 
    } 
     [picker dismissModalViewControllerAnimated:YES]; 
}