【问题标题】:load photo from device in iOS4在 iOS4 中从设备加载照片
【发布时间】:2010-07-24 01:59:43
【问题描述】:

我想一次从PhotoAlbum 加载UIImages 的计数并显示在 UIView 上。 在 iOS3 中,它可能会喜欢:

NSString *file_path = @"/var/mobile/Media/DCIM/100APPLE/" 
NSArray *imageSource = [fileManager contentsOfDirectoryAtPath:file_path error:nil] ; 

UIImage *image = [UIImage imageWithContentsOfFile:[imageSource objectAtIndex:index]] ;
if (!image) {
NSLog(@"load image fail : %@" ,[imageSource objectAtIndex:index]) ;
}
return image ;

在 iOS3 中,这种方法是可行的! 但现在在 iOS4 中,我知道路径已更改为

@"/var/mobile/Media/PhotoData/100APPLE/"

imageSource 可以解析为 2 种类型的文件:.BTH(full size).THM(thumbnail),但它不能[UIImage imageWithContentsOfFile:XXX.BTH]读取BTH

这个问题有什么解决办法吗?或者这种方法应该被 Apple 拒绝?

非常感谢!

【问题讨论】:

    标签: iphone uiimage ios4 photo


    【解决方案1】:

    我认为您不允许进入文件系统获取照片,您的应用将被彻底拒绝。你应该尝试这样的事情:

    -(IBAction)openAlbums {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIImagePickerController *UIPicker = [[UIImagePickerController alloc] init];
            UIPicker.allowsImageEditing = NO;
            UIPicker.delegate = self;
            UIPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentModalViewController:UIPicker animated:YES];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }
    

    当然,这不是特别想要使用的确切代码,所以只需根据您的需要进行修改。

    【讨论】:

      【解决方案2】:

      实际上,我没有找到更好的方法在我自己的控制器中显示整个图像。 我调整流程以满足我的要求。

       - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)image editingInfo:(NSDictionary *)editingInfo{
      
       // Transform image to NSData
       NSData *imageData = UIImagePNGRepresentation(image);
      
       // Create your path with Customized file-name   
       NSString *paths=NSHomeDirectory();   
       NSString *filename=@"XXXX.png";
       NSString *save=[paths stringByAppendingPathComponent:filename];   
      
       [imageData writeToFile:save atomically:NO];   
      
       [picker dismissModalViewControllerAnimated:YES];  
      

      }

      该方法在用户单击缩略图时发生。如果你想执行多选,我的解决方案是使用 NSOperation 来处理文件保存,它仍然需要用户点击缩略图。
      顺便说一句,它可能需要在 UIImagePNGRepresentation(image) 之后缩放照片图像,原始图像(1200x1600)几乎 300Kb

      【讨论】:

        【解决方案3】:

        使用这两种方法加载您的照片表单设备。

        -(void)takePhoto:(id)sender
         {
          UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        
          //Use camera if device has one otherwise use photo library
          if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
           {
             [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
           }
          else
          {
            [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
           }
        
           [imagePicker setDelegate:self];
        
           //Show image picker
           [self presentModalViewController:imagePicker animated:YES];
           //Modal so we wait for it to complete
           [imagePicker release];
             }
        
          //********** RECEIVE PICTURE **********
          - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
          { 
          //Get image
            UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
          //Display in ImageView object (if you want to display it
            [playerImage setImage:image];
        
          //Take image picker off the screen (required)
            [self dismissModalViewControllerAnimated:YES];
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-31
          • 2011-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-03
          相关资源
          最近更新 更多