iphone images are usually stored in 4 places "album, app Package, sandbox, Internet", through these 4 sources, we can access the application image.
Album
The iphone album contains some photos of camera roll + user computer syncing. Users can select an image from an album by using the interactive dialog box provided by the Uiimagepickercontroller class. However, note: The picture Machine path in the album cannot be accessed directly from the application, only through the end user to select and use the photo album
Application Package
An 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.
Sand box
With the sandbox, we can store images in documents, libraries, and TMP folders. These files can have application reads, and images can be created from file paths. Although the outside of the sandbox is technically feasible, Apple indicates that these parts are not within the scope of the AppStore application.
Internet
Applications can access resources on the Internet through the URL of the picture.
The above for some small knowledge, from the "iphone Development Cheats (second edition)", you can refer to this book yourself.
The following starts to cut to the chase, from the camera/album to get pictures, compress pictures, upload pictures.
Get pictures from Camera/album
Just mentioned in the above knowledge from the camera/album to get the picture is for the end user, by the user to browse and select the image for the program to use. Here we need the Uiimagepickercontroller class to interact with the user.
Using 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 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 to get the picture from the album, we first instantiate the Uiimagepickercontroller object, then set the Imagepicker object as the current object, Set Imagepicker's picture source as Uiimagepickercontrollersourcetypephotolibrary, indicating that the current picture is from a photo album, in addition to setting whether the user can edit the picture.
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 from the album to get pictures just picture source settings are not the same, the camera image from the source for Uiimagepickercontrollersourcetypecamera.
After interacting with the user, the user selects a good picture and then recalls the method of selecting end.
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];
}
At the end of the callback method, we handle the size of the image and prepare the image for uploading.
Zoom picture
Zooming the picture is simple, put the code directly, let us refer to it.
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 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;
}
Storing images
In the above we obtained the picture and compressed the picture, through the previous knowledge, the application needs to save some of the pictures to sandbox is a good choice, and the application can go directly through the path to the method sandbox picture, where we put the picture in the sandbox in the documents directory.
View Code
The code is as follows
#pragma mark save picture 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 get 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 get the picture from the documents below, we first need to get the path to the documents directory.
View Code
The code is as follows
#pragma mark gets the documents path from the document directory
-(NSString *) Documentfolderpath
{
return [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents"];
}
Then, we can access the resource by the file name.
View Code
Upload image
In the project we used the Asiformhttprequest open source framework, and some of the HTTP request code is as follows, and 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];
}
recently called System albums, Camera discovery is in EnglishSystem album interface after the title display "Photos", but the phone language has been set to display Chinese, tangled half a day, eventually inInfo.plist Settings Fix the problem
Info.plist inside add localized resources can be mixed YES
Indicates whether the application is allowed to get the language within the framework library.
iOS calls album, Camera, upload, and other issues