好的,我已经想通了。Assets Library 的问题在于它会提供图像的所有 GEO 数据。对于使用您的应用程序的用户来说,这意味着他们将收到提示,称您的应用程序正在尝试访问他们的位置。实际上,你只是想让他们从照片库中选择多个图片。大多数用户会认为这是一种盗版问题而感到反感。最好的方法是使用苹果的 imagePickerController api。我知道它可以让你一次选择一张图片,但如果你添加下面的代码,它将让你选择多张图片。
我的做法是让用户继续选择他们想要的图片,将这些文件保存在应用程序文档目录中,直到他们点击完成按钮。 这里是我的示例代码,希望能为您节省浏览 Assets Library 的痛苦。
-(IBAction)selectExitingPicture
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popoverController presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 300.0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if (!doneButton)
{
doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleDone
target:self action:@selector(saveImagesDone:)];
}
viewController.navigationItem.rightBarButtonItem = doneButton;
}
- (IBAction)saveImagesDone:(id)sender
{
[popoverController dismissPopoverAnimated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
IMAGE_COUNTER = IMAGE_COUNTER + 1;
imageView.image = image;
NSData* imageData = UIImageJPEGRepresentation(image, 1.0);
NSString* incrementedImgStr = [NSString stringWithFormat: @"UserCustomPotraitPic%d.jpg", IMAGE_COUNTER];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr];
[imageData writeToFile:fullPathToFile2 atomically:NO];
}
//现在使用这段代码获取用户选择的图片。您可以从代码中任意位置调用它。
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
NSString* documentsPath = [paths objectAtIndex:0];
NSString* dataFile = [documentsPath stringByAppendingPathComponent:@"UserCustomPotraitPic1.jpg"];
NSData *potraitImgData = [NSData dataWithContentsOfFile:dataFile];
backgroundImagePotrait = [UIImage imageWithData:potraitImgData];