将库图像复制到项目文件Objective-C

时间:2015-09-29 09:47:00

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

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

3 个答案:

答案 0 :(得分:0)

使用UIimagePicker

public static void install(byte[] bArray, short bOffset, byte bLength) {
    byte aidLength = bArray[bOffset];
    short controlLength = (short)(bArray[(short)(bOffset+1+aidLength)]&(short)0x00FF);
    short dataLength = (short)(bArray[(short)(bOffset+1+aidLength+1+controlLength)]&(short)0x00FF);
    new MyPinCard(bArray, (short) (bOffset+1+aidLength+1+controlLength+1), dataLength).register(bArray, (short) (bOffset + 1), aidLength);
}

private MyPinCard(byte[] bArray, short bOffset, short bLength) {
    if(bLength!=(short)(2)) {
        ISOException.throwIt(ISO7816.SW_WRONG_DATA);
    }
    // ....
    m_pin = new OwnerPIN(PIN_TRY_LIMIT, (byte)2);
    m_pin.update(bArray, bOffset, (byte)2);
    // ....
}

答案 1 :(得分:0)

要实现此目的,请执行以下步骤。

  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];
    }
    
  2. 您将在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
      }
      
    2. 获取文档路径

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

答案 2 :(得分:0)

根据需要在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];
}

我希望它有所帮助......