2011-09-30 14 views
12

मैं ऐसे एप्लिकेशन पर काम कर रहा हूं जो लगभग यहां मिले नमूना कोड जैसा ही है। लेकिन जिस विधि को मैं करना चाहता हूं वह नमूना कोड से अलग है।
लिंक: PhotoLocationsफोटो गैलरी से फ़ोटो कैसे लोड करें और इसे एप्लिकेशन प्रोजेक्ट में स्टोर करें?

पहली स्क्रीन में एक छवि दृश्य और 2 बटन होंगे (फोटो चुनें, पुष्टि करें)। जब 'फोटो चुनें' बटन टैप किया जाता है, तो यह दूसरी स्क्रीन पर नेविगेट करेगा जो मेरी आईपैड की फोटो गैलरी से फ़ोटो पुनर्प्राप्त करेगा। जब कोई फोटो चुना जाता है, तो यह वर्तमान स्क्रीन को खारिज कर देगा और ImageView पर चुनी गई तस्वीर को प्रदर्शित करने वाली पहली स्क्रीन पर वापस आ जाएगा।

जब 'पुष्टि करें' बटन टैप किया जाता है, तो फ़ोटो मेरे एप्लिकेशन की प्रोजेक्ट (उदा। /resources/images/photo.jpg) में संग्रहीत की जाएगी।

मई मुझे पता है कि मैं यह कैसे कर सकता हूं?

+0

iPhone में आप UIImagePickerController उपयोग करने के लिए गैलरी से छवियों को पुनः प्राप्त करने और आप NSDocumentsDirectory – booleanBoy

+0

हाँ में सहेजने का धन्यवाद द्वारा उर एप्लिकेशन में उन्हें हो सकता है, लेकिन मैं कुछ नमूना कोड इतना पसंद करेंगे कि मैं देख सकता हूँ। – Lloydworth

+2

google "UIImagePickerController संदर्भ"। ऐप्पल की वेबसाइट पर जाएं। – Akshay

उत्तर

32

एल्बम से फ़ोटो को यू चयन वापस आ जाएगी यह छवि गैलरी के पास ले जाएगा और आप छवि का चयन कर सकते हैं।

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init]; 
imagePickerController.delegate = self; 
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
[self presentViewController:imagePickerController animated:YES completion:nil]; 

इस छवि को अपने आप को इस

का उपयोग क्लिक करने के लिए मदद से आप छवि

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image 
       editingInfo:(NSDictionary *)editingInfo 
{ 
    // Dismiss the image selection, hide the picker and 

    //show the image view with the picked image 

    [picker dismissViewControllerAnimated:YES completion:nil]; 
    //UIImage *newImage = image; 
} 

चयन और फिर आप दस्तावेजों निर्देशिका के लिए इस छवि को स्टोर कर सकते ...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; 
UIImage *image = imageView.image; // imageView is my image from camera 
NSData *imageData = UIImagePNGRepresentation(image); 
[imageData writeToFile:savedImagePath atomically:NO]; 

- (IBAction) takePhoto:(id) sender 
{ 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 

    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 

    [self presentModalViewController:imagePickerController animated:YES]; 
} 
+0

क्या मुझे पता है कि मैं छवि दृश्य से छवि निर्देशिका में छवि को कैसे संग्रहीत कर सकता हूं? – Lloydworth

+2

बस मेरे उत्तर को ऊपर संपादित किया गया ... –

+0

मैंने अभी एक अतिरिक्त फ़ंक्शन के बारे में सोचा था। पहली स्क्रीन में, मैं 'फोटो गैलरी से ब्राउज़ करें' पर 'फोटो चुनें' बटन बदल दूंगा और एक नया बटन 'एक नई तस्वीर लें' भी जोड़ूंगा। उस बटन पर टैप करके, मेरा एप्लिकेशन कैमरा लॉन्च करेगा और उपयोगकर्ता एक तस्वीर ले लेगा, यह छवि स्क्रीन पर चित्र दिखाते हुए पहली स्क्रीन पर वापस आ जाएगा जिसे मेरी दस्तावेज़ निर्देशिका में भी सहेजा जा सकता है। – Lloydworth

3
UIImagePickerController *cardPicker = [[UIImagePickerController alloc]init]; 
cardPicker.allowsEditing=YES; 
cardPicker.delegate=self; 
cardPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
[self presentModalViewController:appDelegate.cardPicker animated:YES]; 
[cardPicker release]; 

और यह प्रतिनिधि विधि

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{ 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 
    noImage.image=img; 
} 
+0

जहां कोई छवि UIImageView है जिसमें मैं एल्बम से चुनी गई छवि प्रदर्शित कर रहा हूं ... – booleanBoy

+0

क्या मुझे पता है कि मैं दस्तावेज निर्देशिका में कोई छवि कैसे संग्रहीत कर सकता हूं? – Lloydworth

0

- (IBAction)UploadPhoto:(id)sender { 
 
    
 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please Select Your Option"delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Gallery",nil]; 
 
    
 
    [actionSheet showInView:self.view]; 
 
    
 
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview]; 
 
    
 
    NSIndexPath *clickedButtonPath = [jobstable indexPathForCell:clickedCell]; 
 
    
 
    isSectionIndex = clickedButtonPath.section; 
 
    isRowIndex  = clickedButtonPath.row; 
 
    
 
} 
 

 

 
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
 
    
 
    
 
    NSLog(@"From didDismissWithButtonIndex - Selected Option: %@", [actionSheet buttonTitleAtIndex:buttonIndex]); 
 
    
 
    NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex]; 
 
    
 
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"]) { 
 
     
 
     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
 
     { 
 
      UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
 
      [alert show]; 
 
      return; 
 
     } 
 
     else{ 
 
      
 
      UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
 
      picker.delegate = self; 
 
      picker.allowsEditing = YES; 
 
      picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
 
      
 
      [self presentViewController:picker animated:YES completion:NULL]; 
 
      
 
     } 
 
    } 
 
    
 
    else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Gallery"]){ 
 
     
 
     
 
     UIImagePickerController *pickerView = [[UIImagePickerController alloc] init]; 
 
     pickerView.allowsEditing = YES; 
 
     pickerView.delegate = self; 
 
     [pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]; 
 
     [self presentViewController:pickerView animated:YES completion:nil]; 
 
     
 
    } 
 
    
 
} 
 

 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
 
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 
 
     
 
     
 
     
 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex]; 
 
     
 
     UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath]; 
 
     
 
     UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19]; 
 
     
 
     tableIMAGE.image=orginalImage; 
 
     
 
    answersARRAY[indexPath.row] = [NSString stringWithFormat:@"-1,%@,%@,",answersARRAY[indexPath.row],imageStris]; 
 

 
    
 
     
 
     [self dismissViewControllerAnimated:YES completion:nil]; 
 
     
 
     
 
    } 
 
    
 
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
 
     
 
     [picker dismissViewControllerAnimated:YES completion:NULL]; 
 
    
 
    }

+0

यह पहले से ही स्वीकृत उत्तर के साथ पुराने प्रश्न का एक नया उत्तर है। आपके उत्तर ने क्या प्रदान किया है कि स्वीकृत उत्तर नहीं है? इसके अतिरिक्त, यह कोड कैसे काम करता है इस बारे में एक स्पष्टीकरण जोड़ना भविष्य के आगंतुकों की सहायता करेगा। – JAL

संबंधित मुद्दे