Sunday, September 8, 2013

ImagePicker Delegate Methods

ImagePicker Delegate Methods :-

-(void)SaveImage
{
 if (imageView.image != nil) {
            
            UIImage *image = imageView.image;
            
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            NSString *strUser = [prefs valueForKey:@"username"];
            [strUser retain];
            NSString * strUserLocal = [objinsurance.insurance_id stringByAppendingString:@".png"];
            
            NSString *imagePath = [[appDelegate applicationDocumentCacheDirectory] stringByAppendingPathComponent:strUserLocal];
            NSLog(@"Path by Image To SAVE :::::>>>%@<<<<<<",imagePath);
            
            NSData *data = [NSData dataWithData:UIImagePNGRepresentation(image)];
            
            if(data != nil) {
                [data writeToFile:imagePath atomically:YES];
                NSLog(@">>>>>>>>>>>>>>>>>>> Image Going To Write <<<<<<<<<<<");
                
               objinsurance.picture = imagePath;
            }
            else
            {
                [[NSFileManager defaultManager] removeItemAtPath:imagePath error:NULL];
                NSLog(@">>>>>>>>>>>>>>>>>>>   Error Writing Image File to Directory      <<<<<<<<<<<");
            }
            
        }else{
            
            NSLog(@"nil");
        }
}

-(IBAction)onClickPicture:(id)sender
{
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Attach A Picture" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take Picture From Camera",@"Picture From gallery",nil];
    action.actionSheetStyle = UIActionSheetStyleDefault;
    [action showInView:self.tabBarController.tabBar];
    [action release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
    if (buttonIndex == 0)
    {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])  {
            imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imgPicker.showsCameraControls = YES;
            imgPicker.wantsFullScreenLayout = YES;
            imgPicker.delegate = self;
            
            CGAffineTransform cameraTransform = CGAffineTransformMakeScale(1.132, 1.132);
            imgPicker.cameraViewTransform = cameraTransform;
            
            UIView *headsUpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
            [imgPicker setCameraOverlayView:headsUpView];
            
            [self presentModalViewController:imgPicker animated:YES];
            [imgPicker release];
            [headsUpView release];
        }
        else
        {
            UIAlertView *altnot=[[UIAlertView alloc]initWithTitle:@"Camera Not Available" message:@"Camera Not Available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            altnot.tag=103;
            [altnot show];
            [altnot release];
        }
    }
    else if(buttonIndex == 1)
    {
        imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imgPicker.delegate = self;
        [self presentViewController:imgPicker animated:YES completion:nil];
        [imgPicker release];
    }
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    UIImage *tempImg= [appDelegate scaleAndRotateImage:image];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    imageView.image = tempImg;

}

No comments:

Post a Comment