2012-07-18 70 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

你使用ARC後委託方法

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

下方的UIImagePickerController委託方法?因爲我相對確定你有記憶問題。 – Jake 2012-07-18 16:00:32

+0

你使用ARC嗎?你是否使用picker delegate沒有設置爲零?你能發佈你看到的錯誤消息嗎? – 2012-07-18 16:01:21

+0

是的,我正在使用ARC。控制檯中的調試器沒有顯示錯誤。它只是終止應用程序,有時它的白色屏幕。 – Rahul 2012-07-18 16:06:06

回答

0

白屏問題通過將UIIMagePickerController委託方法返回的圖像保存到@autoreleasepool(適用於iOS5)中解決。它解決了這個問題,我們不能在ARC代碼中使用NSAutoreleasePool。

下面的代碼didFinishPickingMediaWithInfo行:實施@autoreleasepool

- (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]; 
} 
相關問題