iOS camera/album Get pictures, Compress pictures, upload servers

Source: Internet
Author: User

These days in the iphone on the development of an application, there is a need for camera/album programming and image upload issues, here to summarize. "Part of the knowledge" 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 album Picture app 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. With Uiimagepickercontroller and user interaction, we need to implement 2 protocols <uiimagepickercontrollerdelegate,uinavigationcontrollerdelegate >. View Code tr>
copy code
#pragma mark Get active picture from 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 tr>
copy code
#pragma mark Get 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 Copy Code
-(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 Copy Code
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 Copy Code
#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 Copy Code
#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 Copy Code
-(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];
}

iOS camera/album Get pictures, Compress pictures, upload servers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.