Ios7 如何在iOS 7中通过提供图像名称从照片库中获取图像
我想从iPhone模拟器中的照片库中获取图像,只需给出图像名称或完整路径。但我不知道该怎么做。请带我离开这里。Ios7 如何在iOS 7中通过提供图像名称从照片库中获取图像,ios7,Ios7,我想从iPhone模拟器中的照片库中获取图像,只需给出图像名称或完整路径。但我不知道该怎么做。请带我离开这里。 提前谢谢。要访问库目录,您需要使用NSLibraryDirectory而不是NSDocumentDirectory,然后使用以下代码。我想这可能对你有用 NSString* fileName = [NSString stringWithFormat:@"%@.png",patientlastName]; NSArray *arrayPaths = NSSearchPathForDire
提前谢谢。要访问库目录,您需要使用NSLibraryDirectory而不是NSDocumentDirectory,然后使用以下代码。我想这可能对你有用
NSString* fileName = [NSString stringWithFormat:@"%@.png",patientlastName];
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
UIImage *image1=[UIImage imageWithContentsOfFile:pdfFileName];
imageView.image=image1;
如果您想打开照片库并从中获取图像,则必须使用UIImagePickerController并实现其委托方法,如下所示
在.m文件中类的@implementation上面,使用以下代码
#pragma mark - NON Rotation
@interface NonRotatingUIImagePickerController : UIImagePickerController
@end
@implementation NonRotatingUIImagePickerController
- (BOOL)shouldAutorotate
{
return NO;
}
@end
然后在实现class.m文件时使用下面的代码
-(void)openPhotoLibrary {
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imagePicker =
[[NonRotatingUIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
}
}
UIImagePickerController代理
#pragma mark - ImagePicker Controller Delegate
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
}
}
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
{
if (error) {
//Right some error related code...
}
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
我认为这会对您有所帮助。您是否需要在不打开照片库的情况下从照片库获取图像?或者您需要通过拾取照片库中的图像来获取照片库中的图像。照片库将被打开?如果您要从照片库中打开图像,则必须使用uiimagepicker控制器并实现其委派方法。要访问照片库,您必须使用以下方法。NSArray*Path=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,是);NSString*libPath=[path objectAtIndex:0];
NSString* fileName = [NSString stringWithFormat:@"%@.png",patientlastName];
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
UIImage *image1=[UIImage imageWithContentsOfFile:pdfFileName];
imageView.image=image1;