我在iPhone中动态创建按钮,但我想在iPhone中设置画廊的背景。现在我想知道如何从iPhone中的图库中获取图像。
答案 0 :(得分:3)
-(IBAction)btnSelectPhotoClick:(id)sender{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Photo Library Does Not Exist"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
// set it in image view
imageview.image = image;
// If it's from camera, save it!
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera){
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
}
[picker dismissModalViewControllerAnimated:YES];
}
// Catch when it's canceled
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
希望如果您有任何疑问,请告诉我,这将对您有所帮助。谢谢
答案 1 :(得分:1)
如果您有机会为用户选择图像,那么UIImagePickerController将非常有用。有关详情,请使用此网址http://blog.hanpo.tw/2012/01/uiimagepickercontroller-and-simple.html
答案 2 :(得分:1)
你可以从这样的图库中获取图像
- (void)choosePhotos{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
YourImageView.image = image;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}