I have been developing an application on the iphone over the past few days. There are problems that require camera/album programming and Image Upload. Here I will summarize them.
[Partial knowledge]
Images on the iphone are usually stored in four locations: [photo album, application package, sandbox, and Internet]. With these four sources, we can access application images.
Album
The iphone photo album contains some photos synchronized from the camera film + the user's computer. You can select an image from the album through the interaction dialog box provided by the UIImagePickerController class. However, note: The image machine path in the album cannot be directly accessed from the application. You can only select and use the album image through the end user.
Application Package
The application package may store images with executable programs, Info. plist files, and other resources. We can read these package-based images through the local file path and display them in the application.
Sandbox
With sandbox, we can store images in the Documents, Library, and tmp folders. These files can be read by applications and images can be created through file paths. Although the parts outside the sandbox are technically feasible, apple indicates that these parts are not within the scope allowed by the appstore application.
Internet
Applications can access resources on the Internet through the image URL.
The above is a little bit of knowledge, from the iphone development cheat book (second edition), you can refer to this book by yourself.
Next, let's start with the question: get an image from the camera/album, compress the image, and upload the image.
Obtain images from the camera/album
As mentioned above, obtaining images from cameras/albums is intended for end users. Users can browse and select images as programs. Here, we need to use the UIImagePickerController class to interact with users.
To use UIImagePickerController to interact with users, we need to implement two protocols.
View Code
The Code is as follows:
# Pragma mark obtains activity images from the user's album
-(Void) pickImageFromAlbum
{
ImagePicker = [[UIImagePickerController alloc] init];
ImagePicker. delegate = self;
ImagePicker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ImagePicker. modalTransitionStyle = UIModalTransitionStyleCoverVertical;
ImagePicker. allowsEditing = YES;
[Self presentModalViewController: imagePicker animated: YES];
}
Let's take a look at the above picture retrieval from the album. First, we need to instantiate the UIImagePickerController object, set the imagePicker object to the current object, and set the imagePicker image source to UIImagePickerControllerSourceTypePhotoLibrary, indicates that the source of the current image is an album. In addition, you can also set whether the image can be edited.
View Code
The Code is as follows:
# Pragma mark obtains activity images from the camera
-(Void) pickImageFromCamera
{
ImagePicker = [[UIImagePickerController alloc] init];
ImagePicker. delegate = self;
ImagePicker. sourceType = UIImagePickerControllerSourceTypeCamera;
ImagePicker. modalTransitionStyle = UIModalTransitionStyleCoverVertical;
ImagePicker. allowsEditing = YES;
[Self presentModalViewController: imagePicker animated: YES];
}
The above figure shows how to get an image from the camera. It is different from how to get an image from the album. The source of the camera image is UIImagePickerControllerSourceTypeCamera.
After interacting with the user, after the user selects an image, the method of ending is called back.
View Code
The Code is as follows:
-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info
{
UIImage * image = [info objectForKey: @ "UIImagePickerControllerOriginalImage"];
If (picker. sourceType = UIImagePickerControllerSourceTypeCamera)
{
// UIImageWriteToSavedPhotosAlbum (image, nil );
}
TheImage = [UtilMethod imageWithImageSimple: image scaledToSize: CGSizeMake (120.0, 120.0)];
UIImage * midImage = [UtilMethod imageWithImageSimple: image scaledToSize: CGSizeMake (210.0, 210.0)];
UIImage * bigImage = [UtilMethod imageWithImageSimple: image scaledToSize: CGSizeMake (440.0, 440.0)];
[TheImage retain];
[Self saveImage: theImage WithName: @ "salesImageSmall.jpg"];
[Self saveImage: midImage WithName: @ "salesImageMid.jpg"];
[Self saveImage: bigImage WithName: @ "salesImageBig.jpg"];
[Self dismissModalViewControllerAnimated: YES];
[Self refreshData];
[Picker release];
}
In the callback end method, the image size is processed to prepare for uploading the image.
Zoom Image
It is relatively simple to scale the image, so you can directly put the code for your reference.
View Code
The Code is as follows:
// Compress the image
+ (UIImage *) imageWithImageSimple :( UIImage *) image scaledToSize :( CGSize) newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext (newSize );
// Tell the old image to draw in this new context, with the desired
// New size
[Image drawInRect: CGRectMake (0, 0, newSize. width, newSize. height)];
// Get the new image from the context
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext ();
// End the context
UIGraphicsEndImageContext ();
// Return the new image.
Return newImage;
}
Store images
We have obtained and compressed the image above, and learned from the previous tips that it is a good choice to save some of the images required by the application to the sandbox, in addition, the application can directly use the path to access the images in the sandbox. Here we store the images in the Documents directory in the sandbox.
View Code
The Code is as follows:
# Pragma mark Save the image to the document
-(Void) saveImage :( UIImage *) tempImage WithName :( NSString *) imageName
{
NSData * imageData = UIImagePNGRepresentation (tempImage );
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * documentsDirectory = [paths objectAtIndex: 0];
// Now we get the full path to the file
NSString * fullPathToFile = [documentsDirectory stringByAppendingPathComponent: imageName];
// And then we write it out
[ImageData writeToFile: fullPathToFile atomically: NO];
}
Retrieve images from the Documents directory
To obtain an image from Documents, we first need to obtain the path of the Documents directory.
View Code
The Code is as follows:
# Pragma mark obtains the Documents path from the document directory
-(NSString *) documentFolderPath
{
Return [NSHomeDirectory () stringByAppendingPathComponent: @ "events"];
}
Then, we can access the resources through the file name.
View Code
Upload images
In the project, we use the ASIFormHttpRequest open-source framework. Part of the http Request Code is as follows. The http return and related callback methods are omitted.
View Code
The Code is as follows:
-(Void) upLoadSalesBigImage :( NSString *) bigImage MidImage :( NSString *) midImage SmallImage :( NSString *) smallImage
{
NSURL * url = [NSURL URLWithString: UPLOAD_SERVER_URL];
ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL: url];
[Request setPostValue: @ "photo" forKey: @ "type"];
[Request setFile: bigImage forKey: @ "file_pic_big"];
[Request buildPostBody];
[Request setDelegate: self];
[Request setTimeOutSeconds: TIME_OUT_SECONDS];
[Request startAsynchronous];
}