These days in the iphone above an application of the development, there is a need for Camera/photo album programming and image upload problems, summed up here.
"Partial Knowledge"
Images in the iphone are usually stored in 4 places, "photo albums, application packages, sandbox, Internet", and through these 4 sources, we can access the application picture.
Album
The iphone's photo album contains some photos of camera film + user computer sync. Users can select images from albums by using the interactive dialog box provided by the Uiimagepickercontroller class. Note, however, that the picture machine path in the album cannot be accessed directly from the application and can only be selected and used by the end-user to select and use photo albums
Application Packages
The application package may store images with executable programs, info.plist files, and other resources. We can read these packages based on the local file path and display them in the application.
Sand box
With the sandbox, we can store the pictures in the documents, Library, and TMP folders. These files can be read by the application, and images can be created from the file path. While the parts outside the sandbox are technically feasible, Apple has shown that these parts are not within the scope of the App Store app that allows access.
Internet
Applications can access resources on the Internet through the URL of a picture.
The above is a little knowledge, from the "iphone Development Cheats (second edition)", you can refer to this book.
Below starts to cut to the chase, obtains the picture from the camera/album, compresses the picture, uploads the picture.
Get pictures from Camera/photo album
Just in the knowledge above mentioned from the camera/album to get the picture is for the end user, the user to browse and select pictures for the program to use. Here we need the Uiimagepickercontroller class to interact with the user.
With Uiimagepickercontroller and user interaction, we need to implement 2 protocols.
View Code
The code is as follows
#pragma mark gets the active picture 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 picture above from the album, we first instantiate the Uiimagepickercontroller object, and then set the Imagepicker object as the current object, Set Imagepicker's picture from Uiimagepickercontrollersourcetypephotolibrary, which indicates the source of the current picture is an album, in addition to the user can also set whether the picture can be edited.
View Code
The code is as follows
#pragma mark gets the active picture 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 is from the camera to get pictures, and get pictures from photo albums just the source of the picture is not the same, the camera image source for Uiimagepickercontrollersourcetypecamera.
After interacting with the user, the user selects a good picture and then recalls the method that ends the selection.
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, nil, 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 end of the callback method, we have the image of the size of the processing, for the upload of pictures to prepare.
Zoom picture
Zoom picture is relatively simple, put the code directly, let everyone refer to.
View Code
The code is as follows
Compress pictures
+ (uiimage*) Imagewithimagesimple: (uiimage*) Image scaledtosize: (cgsize) newsize
{
Create a graphics image context
Uigraphicsbeginimagecontext (newsize);
Tell the ' old image ' to ' draw in the ' 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 of the context
Uigraphicsendimagecontext ();
Return the new image.
return newimage;
}
Storing images
On top we get the picture and compress the picture, learn from the previous knowledge that the application needs of some of the pictures into the sandbox is a good choice, and the application can go directly through the path to the method sandbox in the picture, where we will be in the picture in the sandbox in the documents directory.
View Code
The code is as follows
#pragma mark saves pictures to 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 have the full path to the file
nsstring* fullpathtofile = [Documentsdirectory stringbyappendingpathcomponent:imagename];
And then we write it out
[ImageData writetofile:fullpathtofile Atomically:no];
}
Get pictures from the documents directory
To obtain pictures from documents below, we first need to obtain the path of the documents directory.
View Code
The code is as follows
#pragma mark gets the documents from the document directory
-(NSString *) Documentfolderpath
{
return [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"];
}
Then, we can access the resource through the filename.
View Code
Upload pictures
In the project we used the Asiformhttprequest open source framework, some of the HTTP request code is as follows, HTTP return and the associated callback method is 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];
}