我有一个旧的代码工作,我没有最终使用...现在我再次尝试它,它不起作用。我不确定有什么问题。我希望用户选择一个图像,然后在imageView1中显示它。我很感激你的帮助。
- (IBAction)pushPick
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate =self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
//[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
imageView1.image = image;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
答案 0 :(得分:6)
尝试
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
imageView1.image = image;
[picker release];
[self dismissModalViewControllerAnimated:YES];
}
确保视图控制器响应UIImagePickerControllerDelegate。
imagePickerController:didFinishPickingImage:editingInfo:在iOS 3.0中已弃用。
答案 1 :(得分:0)
我就是这样做的:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{ bgImage.image = [info objectForKey:UIImagePickerControllerEditedImage];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(128, 128), YES, 1.0);
[bgImage.image drawInRect:CGRectMake(0,0,128,128)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsFolder = [path objectAtIndex:0];
NSString *tmpPathToFile = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/specificImagedName.jpg", documentsFolder]];
if([imageData writeToFile:tmpPathToFile atomically:YES]){
//NSLog(@"write succesful");
}
///////////////////////////////////////////
[po dismissPopoverAnimated:YES];
[picker release];
[tmpPathToFile release];}
它会在您应用的文档文件夹中生成一个jpg文件。希望它有所帮助..