2010-07-24 62 views
0

我想从PhotoAlbum一次加载计数UIImages并显示在UIView上。 在IOS3,它可能会喜欢:从iOS4的设备载入照片

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(全尺寸).THM(缩略图),但它不能 阅读BTH from [UIImage imageWithContentsOfFile:XXX.BTH]

有没有解决这个问题的方法?或者这种方法应该从苹果拒绝?

非常感谢!

回答

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

当然,这不会是确切的代码特别是将要使用的,因此只需简单修改您的需求。

1

其实,我发现没有更好的方法来显示整个图像在我自己​​的控制器。 我调整流量以适合我的要求。

- (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 *[email protected]"XXXX.png"; 
NSString *save=[paths stringByAppendingPathComponent:filename]; 

[imageData writeToFile:save atomically:NO]; 

[picker dismissModalViewControllerAnimated:YES]; 

}

的方法,当用户点击的thumbnil发生。如果要执行多选,我的解决方案是使用NSOperation来处理文件保存,它仍然需要用户单击缩略图。
顺便说一下,它可能需要规模后UIImagePNGRepresentation(图像)的照片图像,原始图像(1200x1600)几乎300KB

0

使用这两种方法加载你的照片形式器件。

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