này sẽ đưa bạn đến thư viện hình ảnh và bạn có thể chọn hình ảnh.
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerController animated:YES completion:nil];
này sẽ giúp bạn chọn hình ảnh
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
// Dismiss the image selection, hide the picker and
//show the image view with the picked image
[picker dismissViewControllerAnimated:YES completion:nil];
//UIImage *newImage = image;
}
Và sau đó bạn có thể lưu trữ hình ảnh này vào thư mục tài liệu ...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
UIImage *image = imageView.image; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
Đối với cách nhấn vào hình ảnh bản thân sử dụng
này
- (IBAction) takePhoto:(id) sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePickerController animated:YES];
}
Nguồn
2011-09-30 04:41:47
trong iPhone bạn phải sử dụng UIImagePickerController để lấy hình ảnh từ thư viện và bạn có thể có chúng vào ứng dụng ur bằng cách tiết kiệm nó trong NSDocumentsDirectory – booleanBoy
Có cảm ơn bạn, nhưng tôi muốn có một số mã mẫu để mà tôi có thể tham khảo. – Lloydworth
google "Tham chiếu UIImagePickerController". Truy cập trang web của Apple. – Akshay