2015-09-29 6 views

Répondre

0

Utilisez UIimagePicker

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    if ([mediaTypeStr isEqualToString:@"photo"]) 
    { 
     if ([info objectForKey:UIImagePickerControllerOriginalImage]) 
     { 
      // images = [NSMutableArray arrayWithCapacity:[info count]]; 
      _images  = [[NSMutableArray alloc]init]; 
      UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];; 
      img   =[self scaleAndRotateImage:img]; 
      [_images addObject:img]; 

      // Save Photo to library only if it wasnt already saved i.e. its just been taken 
      [self.alAsstlibrary saveImage:img toAlbum:@"FOlder name" completion:^(NSURL *assetURL, NSError *error) 
      { 
       if (error!=nil) 
       { 
        //NSLog(@"Big error: %@", [error description]); 
       } 
      } failure:nil];  
     } 
     else 
     { 
      //NSLog(@"UIImagePickerControllerReferenceURL = %@", info); 
     } 
    } 
    [picker dismissViewControllerAnimated:YES completion:Nil]; 
} 
0

Pour y parvenir, procédez comme suit.

  1. Obtenez l'image

    - (void) getPicture:(id)sender 
    { 
        UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
        picker.delegate = self; 
        picker.allowsEditing = YES; 
        picker.sourceType = (sender == myPic) ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
        [self presentModalViewController:picker animated:YES]; 
        [picker release]; 
    } 
    

Vous obtiendrez l'image délégué imagePickerController

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage (UIImage *)image editingInfo:(NSDictionary *)editingInfo 
{ 
    [self save:image]; 
    [picker dismissModalViewControllerAnimated:YES]; 
} 
  1. Enregistrer l'image dans les documents

    -(void)save:(UIImage*)image 
    { 
        NSData *pngData = UIImagePNGRepresentation(image); 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory 
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name 
        [pngData writeToFile:filePath atomically:YES]; //Write the file 
    } 
    

Get chemin du document

- (NSString *)documentsPathForFileName:(NSString *)name 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 

    return [documentsPath stringByAppendingPathComponent:name]; 
} 
  1. obtenir l'image

    NSData *pngData = [NSData dataWithContentsOfFile:filePath]; 
    UIImage *image = [UIImage imageWithData:pngData]; 
    
+0

quand je l'appelle GetPicture? @Imran – Louis

+0

Chaque fois que vous avez besoin d'obtenir l'image et l'utiliser – Imran

0

écrire ces derniers dans viewDidLoad ou sur les boutons cliquez sur l'événement selon votre besoin ..

UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
picker.delegate = self; 
picker.allowsEditing = NO;//by writing YES here you can edit image also.. 
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 

[self presentViewController:picker animated:YES completion:NULL]; 


imgview=[[UIImageView alloc]initWithFrame:CGRectMake(x+y, y, width, height)]; 
[imgview setTag:i]; 
imgview.contentMode = UIViewContentModeScaleAspectFill; 
[imgview setClipsToBounds:YES]; 
[self.view addSubview:imgview]; 

écrire dans votre fichier .m ..

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 


    //if you write picker.allowsEditing = YES; than write info[UIImagePickerControllerEditedImage] in BELOW line.... 

    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage]; 
    imgview.image = chosenImage;//set selected image in your imageview 

    [picker dismissViewControllerAnimated:YES completion:NULL]; 

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

j'espère que cela aide ..