【问题标题】:copy gallery image to project files Objective-C将画廊图像复制到项目文件 Objective-C
【发布时间】:2015-09-29 09:46:59
【问题描述】:

我是 Objective-C 的新手,我想从图库中选择一张图片,我想获取名称和扩展名并将该图片复制到我的项目文件夹中。

【问题讨论】:

    标签: ios objective-c image-gallery photo-gallery


    【解决方案1】:

    使用 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];
    }
    

    【讨论】:

      【解决方案2】:

      根据您的需要在 viewdidload 或按钮单击事件中写入这些内容..

      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];
      

      将这些写入您的 .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];
      }
      

      希望对你有帮助..

      【讨论】:

        【解决方案3】:

        要实现这一点,请按照以下步骤操作。

        1. 获取图片

          - (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];
          }
          

        您将在 imagePickerController 委托中获取图像

        - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage (UIImage *)image editingInfo:(NSDictionary *)editingInfo 
        {
            [self save:image];
            [picker dismissModalViewControllerAnimated:YES];
        }
        
        1. 将图像保存在文档中

          -(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
          }
          

        获取文档路径

        - (NSString *)documentsPathForFileName:(NSString *)name
        {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
            NSString *documentsPath = [paths objectAtIndex:0];
        
            return [documentsPath stringByAppendingPathComponent:name]; 
        }
        
        1. 获取图片

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

        【讨论】:

        • 当我调用 getPicture 时? @Imran
        • 随时随地获取图像并使用它
        猜你喜欢
        • 2012-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-23
        • 2018-06-18
        • 1970-01-01
        相关资源
        最近更新 更多