Camera/Photo code
by Ecliptic · in iTorque 2D · 02/21/2009 (2:33 pm) · 36 replies
Hey guys! I have been digging around trying to see how I can access the photo library on the iPhone and I found the code how to access it as well as utilizing the camera. I thought I would post here to share with the rest of you if you needed it for later use. Now this is not Torque script, it is the Object-C code that is used on the iphone. I am not sure how you would go about and add this to torque but in the even you need it here you go.
//Make sure the Library isn't empty
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;
picker.delegate = self;
[currentViewcontroller presentModalViewController:picker animated:YES];
}
//To use the Camera use similar code. Just replace source type with Camera.
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.allowsImageEditing = YES;
picker.delegate = self;
[currentViewcontroller presentModalViewController:picker animated:YES];
}
//The assigned Delegate must use this method to recieve the controller.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
//work with the finished image as needed.
[self saveImage:image];
//or you maye Get the original image
UIImage *originalImage = [editingInfo valueForKey:UIImagePickerControllerOriginalImage];
//Save the edit crop as metadata
[self saveTagZone:[editingInfo valueForKey:UIImagePickerControllerCropRect]];
}
#22
02/26/2009 (11:48 am)
@Ecliptic - Nice find! I definitely can take that and our previous findings to a fully working code chunk this weekend. I'm really excited for the weekend now =)
#23
02/26/2009 (4:00 pm)
That would be awesome, I have actually been playing around with Object-c and I have an Xcode project I could give you that allows browsing the photolibrary, selecting an image and editing it to be stored in a variable. I think I can change one string of it to make it bring up the camera instead and take a picture then edit the actual picture you've taken to store. I need to run out right now but when I get back I will post up the .m and header file.
#24
This is the ImagePickerViewController.h file....
This is the ImagePickerViewController.m file
02/26/2009 (7:29 pm)
Here is my files. If you need the xcode project I can give you that if you think it will help. The xib is a simple button and a ImageView from the library for "theImage" to show up in. This is the ImagePickerViewController.h file....
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface ImagePickerViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate> {
IBOutlet UIButton *button;
IBOutlet UIImageView *ImageView;
}
- (IBAction)openPicker;IBOutlet UIImageView *ImageView;
-(void)useImage:(UIImage *)theImage;
@endThis is the ImagePickerViewController.m file
[ImagePickerViewController.m file]
#import "ImagePickerViewController.h"
@implementation ImagePickerViewController
- (IBAction)openPicker {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
}
}
-(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[self useImage:image];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
-(void)useImage:(UIImage *)theImage {
ImageView.image = theImage;
}
@end
#25
02/28/2009 (11:49 am)
@Ecliptic - Is the above code a simple stand-alone project or an integration into iTGB?
#26
I know that once we have the camera working it will save photos to the library, but what do we do once we can choose pics from the lib? The data is stored in a UIImage, a structure/class I'm not familiar with.
Do we want to just display the static image? Render the image to a GUI? Render the image to a background scroller? Create some kind of "eye toy" game?
It's important we have a destination to work toward, otherwise we might code ourselves into a corner.
02/28/2009 (11:56 am)
Another question: Where do we want to go once we have the Photo Library Working?I know that once we have the camera working it will save photos to the library, but what do we do once we can choose pics from the lib? The data is stored in a UIImage, a structure/class I'm not familiar with.
Do we want to just display the static image? Render the image to a GUI? Render the image to a background scroller? Create some kind of "eye toy" game?
It's important we have a destination to work toward, otherwise we might code ourselves into a corner.
#27
That code above is a stand alone code that was coded using the XCode template fromt he iPhone SDK.. I have been trying to figure out how to take that and convert it into something torque can understand but I haven't been too successful.
If you think it will help I will upload the Xcode project for you.
02/28/2009 (1:54 pm)
My original intent was to store the images location in a variable to update my datablocks when the user decides to change an image. But I was thinking if we couldn't do that we could store it in a gui itself.That code above is a stand alone code that was coded using the XCode template fromt he iPhone SDK.. I have been trying to figure out how to take that and convert it into something torque can understand but I haven't been too successful.
If you think it will help I will upload the Xcode project for you.
#28
From the base code I see above, I already have a clear path to follow. Just in case, I should at least run a working copy to see the interaction.
03/01/2009 (11:18 am)
Got my developer hat on today. Go ahead and e-mail me the project zipped up. Shouldn't take me long to integrate into iTGB once I have a sample sitting in front of me.From the base code I see above, I already have a clear path to follow. Just in case, I should at least run a working copy to see the interaction.
#29
After we get the base functionality up, I already know my next module: Video Playback! That will be great for high quality cut scenes, and very simple to implement. I'll make a separate thread for that after we close this one.
03/02/2009 (2:49 pm)
I'm very close to having this finished, and will post my progress this week.After we get the base functionality up, I already know my next module: Video Playback! That will be great for high quality cut scenes, and very simple to implement. I'll make a separate thread for that after we close this one.
#30
03/03/2009 (8:36 am)
That would be very interesting to get video playback! Did you get my email? I sent it to your garage games account.
#31
03/03/2009 (10:40 am)
@Ecliptic - Yes, I got your e-mail but did not respond. Thanks! It's what I needed to help compare the TGB code to a working stand alone app. I'm working on the next version of iTGB right now, so that will take priority temporarily.
#32
I will be posting updates here probably tomorrow. Wish me luck!
Thanks again for the previous help Michael.
Dane
03/12/2009 (8:39 am)
I am about to start working on this code again for those who have been listening to me and Michael ramble :). I have been in the middle of a move (Still am but finally getting time) and decided to get more familliar with object-c. I will actually have two very simple apps waiting for review right now on the app store using the IPhone SDK. Hopefully with a slightly better understanding of how the code works I can achieve the goal of this thread. I will be posting updates here probably tomorrow. Wish me luck!
Thanks again for the previous help Michael.
Dane
#33
Right now I am trying to get it to return the selected image to a particular variable. Once this is complete I will make a new thread with all the files and instructions needed for those of you who would like this feature.
Thanks guys!
Dane
03/18/2009 (12:46 pm)
Well I apologise for the late update. I have been working on multiple things and have been very busy. I finally have the code pulling up the camera or the PhotoLibrary in torque! This is great news :).Right now I am trying to get it to return the selected image to a particular variable. Once this is complete I will make a new thread with all the files and instructions needed for those of you who would like this feature.
Thanks guys!
Dane
#34
I'll keep working on the base iTGB engine and tackle the FMV when there is time.
03/18/2009 (12:49 pm)
Awesome! Great work Ecliptic. I'm sorry I haven't responded lately, as I have been grinding on iTGB 1.2 and prepping for the 3.0 SDK release.I'll keep working on the base iTGB engine and tackle the FMV when there is time.
#35
I setup my XIB file to have a finished button/cancel button so when you select those it removes that XIB file from view. When this happens it loses its data so I wanted to create a statement that will return a value when the user is done.
This is what I got, I would like to return the path of the file selected when if(!finished) returns true. We can use object-c or C++ here so what would be the best way to return this information? If not return the path we could just store it as an instance of the file.. Basically right now we can access the photolibrary and exit it but I am not too sure on how we should go about storing the selected image.
I have been digging through the openFileDialoge but I haven't had much luck so far.
Thanks guys
Dane
03/18/2009 (3:55 pm)
Hey guys.. I was wondering if I could get some feedback or help on this section.I setup my XIB file to have a finished button/cancel button so when you select those it removes that XIB file from view. When this happens it loses its data so I wanted to create a statement that will return a value when the user is done.
void openPhotoViewer()
{
ImagePickerViewController* controller = [[ImagePickerViewController alloc] initWithNibName: @"ImagePickerViewController" bundle: nil];
UIWindow* window = [UIApplication sharedApplication].keyWindow;
[controller loadView];
controller.view.alpha = 0.0f;
[window addSubview: controller.view];
[UIView beginAnimations: nil context: nil];
controller.view.alpha = 1.0f;
[UIView commitAnimations];
bool finished = controller.finished;
if (!finished)
{
//Need to return something here?
}
}This is what I got, I would like to return the path of the file selected when if(!finished) returns true. We can use object-c or C++ here so what would be the best way to return this information? If not return the path we could just store it as an instance of the file.. Basically right now we can access the photolibrary and exit it but I am not too sure on how we should go about storing the selected image.
I have been digging through the openFileDialoge but I haven't had much luck so far.
Thanks guys
Dane
#36
I've never used objective-C before or worked on the iPhone. I'm deciding whether to learn objective-C or buy iTGB but the camera would be the selling point.
07/14/2010 (7:21 pm)
Is this still being worked on? Having a camera in an app I want to make is pretty important.I've never used objective-C before or worked on the iPhone. I'm deciding whether to learn objective-C or buy iTGB but the camera would be the selling point.
Torque Owner Ecliptic
Found this link for info on using the camera for any who wants to play with this. I will try when I finish my photolibrary stuff =D.