Since we need to upload pictures in many places in the project, we can customize uploading image requests, customize photo retrieval and take photos, which is convenient for calling when using multiple places.
Main steps:
1. Step 1: Request to upload the album or photo pictures you selected (compressed)
2. Step 2: Get the first step to upload the image url to the server
3. Step 3: Echo the picture (of course, when entering the interface, first determine whether there are pictures. If there is no picture, the placeholder picture will be displayed, otherwise the picture will be echoed)
Without further ado, just upload the code:
1) Encapsulated uploaded image network request (image compression) QTXUploadImage file
//Upload an image using afn#import <Foundation/> @interface QTXUploadImage : NSObject // Network request for uploading pictures (picture compression)+ (void)post:(NSString *)url image:(UIImage *)image name:(NSString *)name success:(void (^)(id json))success failure:(void (^)(NSError *error))failure; @end
#import "" #import "" #import "" #import "" @implementation QTXUploadImage /** * Network request for uploading pictures (picture compression) * * @param url The network request address for uploading the image * @param name is the same as the background package name * */ + (void)post:(NSString *)url image:(UIImage *)image name:(NSString *)name success:(void (^)(id json))success failure:(void (^)(NSError *error))failure { // 1. Create a network administrator AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // 2. The splicing request parameter url can also be passed in the specific controller request NSDictionary *dict = @{@"userId" : [QTXAccountTool account].userId}; // 3. Send a request [manager POST:url parameters:dict constructingBodyWithBlock: ^void(id<AFMultipartFormData> formData) { NSData *imageData = UIImageJPEGRepresentation(image, 0.5);//Perform image compression // Use date to generate image name NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; = @"yyyyMMddHHmmss"; NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]]; // Arbitrary binary data MIMEType application/octet-stream [formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:@"image/png"]; } success:^void(NSURLSessionDataTask * task, id responseObject) { if (success) { success(responseObject); } } failure:^void(NSURLSessionDataTask * task, NSError * error) { if (failure) { failure(error); } }]; } @end
2) Packaged Photo/Select QTXImagePicker File from Album
// Take a photo/select from the album#import <Foundation/> typedef void(^QTXImagePickerFinishAction)(UIImage *image); @interface QTXImagePicker : NSObject /** @param viewController for present UIImagePickerController object @param allowsEditing Whether to allow users to edit images */ + (void)showImagePickerFromViewController:(UIViewController *)viewController allowsEditing:(BOOL)allowsEditing finishAction:(QTXImagePickerFinishAction)finishAction; @end
#import "" @interface QTXImagePicker()<UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate> @property (nonatomic, weak) UIViewController *viewController; @property (nonatomic, copy) QTXImagePickerFinishAction finishAction; @property (nonatomic, assign) BOOL allowsEditing; @end static QTXImagePicker *qtxImagePickerInstance = nil; @implementation QTXImagePicker + (void)showImagePickerFromViewController:(UIViewController *)viewController allowsEditing:(BOOL)allowsEditing finishAction:(QTXImagePickerFinishAction)finishAction { if (qtxImagePickerInstance == nil) { qtxImagePickerInstance = [[QTXImagePicker alloc] init]; } [qtxImagePickerInstance showImagePickerFromViewController:viewController allowsEditing:allowsEditing finishAction:finishAction]; } - (void)showImagePickerFromViewController:(UIViewController *)viewController allowsEditing:(BOOL)allowsEditing finishAction:(QTXImagePickerFinishAction)finishAction { _viewController = viewController; _finishAction = finishAction; _allowsEditing = allowsEditing; UIActionSheet *sheet = nil; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Photograph", @"Select from album", nil]; }else { sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select from album", nil]; } UIView *window = [UIApplication sharedApplication].keyWindow; [sheet showInView:window]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex]; if ([title isEqualToString:@"Photograph"]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; = self; = UIImagePickerControllerSourceTypeCamera; = _allowsEditing; [_viewController presentViewController:picker animated:YES completion:nil]; }else if ([title isEqualToString:@"Select from album"]) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; // [ setBarTintColor:QTXNavColor]; // Modify the background color of the navigation bar of the album = self; = YES; = UIImagePickerControllerSourceTypePhotoLibrary; [_viewController presentViewController:picker animated:YES completion:nil]; }else { qtxImagePickerInstance = nil; } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = info[UIImagePickerControllerEditedImage]; if (image == nil) { image = info[UIImagePickerControllerOriginalImage]; } if (_finishAction) { _finishAction(image); } [picker dismissViewControllerAnimated:YES completion:^{}]; qtxImagePickerInstance = nil; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { if (_finishAction) { _finishAction(nil); } [picker dismissViewControllerAnimated:YES completion:^{}]; qtxImagePickerInstance = nil; } @end
When used, call it in the controller you want to use:
[QTXImagePicker showImagePickerFromViewController:self allowsEditing:YES finishAction:^(UIImage *image) { if (image) { = image; [QTXUploadImage post:QTX_xsz1Url image:image name:@"xsz1" success:^(id json) { // The first step is to request upload QTXLog(@"Image upload request succeeded %@", json); = json[@"data"]; } failure:^(NSError *error) { QTXLog(@"Student ID image upload request failed %@", error); }]; } }];
Step 2 and Step 3 You need to work with your own background server, which is an ordinary get/post request, so you won't put the code here.
Insert it, our products do not allow the photos taken to be stored in the album
//Storage image name: ~ for (int i = 1; i<=9; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"00%", i]]; UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); [NSThread sleepForTimeInterval:1]; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.