IOS multi album upload pictures, add, delete pictures

Notice: summary of problems that appear when using the method below. Please correct your mistakes when you see them.

Problem 1: someone reflects that when you open the demo, you can’t open the album for the first time. You must restart it to open it.

This problem is caused by lazy ZYQAssetPickerController initialization and can be resolved without using lazy load creation when using

Question two: localization images

Localization of the image in demo has been found to be of no use.

[self.imageDataArray addObject:imageData];

Because this code has to convert the picture into exile bytes in memory, then the localization of a feeling make an unnecessary move

Question three: if the picture is on top of the iCloud, it will crash if you select the picture with ZYQAssetPickerController

If the picture is not local but on iCloud, you need to download the picture locally and then select locally. Can solve the following code in the third party.M added, after adding can solve but end picture in the network under bad circumstances have to wait before you see the selected picture, the method also have to write their own.

RequestOptions.networkAccessAllowed = YES;

The above is the problem, in contrast, after friends recommend a better third party TZImagePickerController, you can go to GitHub download contrast, feel the instant burst demo.

Upload function is common in app radio pictures, more pictures, and also let the show and so on, all demanded a lot, before their own projects were also used in this work can select the time of writing, more anxious, writing is not very good, recently come down to write the first demand. Figure

IOS multi album upload pictures, add, delete pictures
multi image upload.Gif

Think carefully if it does not bother
first multiple-choice album pictures, I use the third party ZYQAssetPickerController interested can go to GitHub search, because this is used in previous projects.
introduces ZYQAssetPickerController and follows the proxy

Self.pickerController [[ZYQAssetPickerController alloc] = init]; _pickerController.maximumNumberOfSelection = 8; _pickerController.assetsFilter = ZYQAssetsFilterAllAssets; _pickerController.showEmptyGroups=NO; _pickerController.delegate=self; _pickerController.selectionFilter = [NSPredicate predicateWithBlock:^BOOL (ID evaluatedObject, NSDictionary *bindings) {if ([(ZYQAsset*) evaluatedObject mediaType]==ZYQAssetMediaTypeVideo) {NSTimeInterval duration = [(ZYQAsset*) evaluatedObject duration]; return duration > else = 5;} {return YES};

Next, present comes out where you need to open the album, so it’s inside of me

[self presentViewController:self.pickerController animated:YES completion:nil];

In this album can choose picture, next is the implementation method of proxy callback

- - (void) assetPickerController: (ZYQAssetPickerController *), picker, didFinishPickingAssets: (NSArray *) assets{

Process your selected pictures here

Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{for (int i=0; i< assets.count; i++) {ZYQAsset *asset=assets[i]; [asset setGetFullScreenImage:^ (UIImage *result) {dispatch_async (dispatch_get_main_queue), iPhone (^{/ / because of taking a photo is too large, directly into the array will cause memory warnings, a serious cause of the crash. So in the image compression, the compression / sandbox, the picture is for your pictures to the server NSData *imageData=UIImageJPEGRepresentation (result, 0.8); [self.imageDataArray addObject:imageData]; [self WriteToBox:imageData]; / / add to display pictures in the array UIImage *image = [self OriginImage:result scaleToSize:CGSizeMake (80, 80)]; [self.imageArray addObject:image]; [self.collectionView reloadData];});}});}]; [self dismissViewControllerAnimated:YES completion:^{[self.collectionView reloadData]}];

Above is the callback from the picture processing agent method, I there are two arrays, one is imageArray, for storage in the collection view shows pictures, one is imageDataArray, used to store the compressed image stream of bytes, we could not have chosen the picture display directly to the collection view, iPhone photos too large (several megabytes), the size is too big, so we put in the view that small ImageView will have a problem, so the first cut pictures, and I put the compressed imageDataArray inside the original store in the sandbox, for later upload to the server
this is stored in the sandbox code

#pragma - mark - WriteToBox: (void) in the sandbox (NSData *) imageData{_i + + NSArray; *filePath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); / / NSString * path for the file Document collectPath = filePath.lastObject; NSFileManager = *fileManager [NSFileManager defaultManager]; if ([fileManager! FileExistsAtPath:collectPath]) {[fileManager createDirectoryAtPath:collectPath withIntermediateDirectories:YES attributes:nil error:nil];} / / / / NSString *newPath = [collectPath splicing new path stringByAppendingPathComponent:[NSString stringWithFormat:@ "Picture_%ld.png" _i]]; NSLog (@ ++%@ ", newPath); [imageData writeToFile:newPath atomic Ally:YES];}

This is a clipping picture for the collection view display

Change the display size - pictures of #pragma mark - (UIImage*) OriginImage: (UIImage *) image scaleToSize: (CGSize size) {UIGraphicsBeginImageContext (size); //size type CGSize, which you need the image size [image drawInRect: CGRectMake (0, 0, size.width, size.height); UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext (UIGraphicsEndImageContext (); return; scaledImage); / / return is changed.}

Again, the collection view shows that the collection view is not created. The main thing is to say the “+” picture and delete the
delete code

#pragma mark: delete pictures - (void) deleteImage: (UIButton *) sender{NSInteger index = sender.tag - 100; / / NSLog (@ index=%ld, index); / / NSLog (@ "+++%ld", self.imageDataArray.count); / / NSLog (@ "---%ld", self.imageArray.count); / / remove display data [self.imageArray removeObjectAtIndex:index] picture array imageArray the data of [self.imageDataArray removeObjectAtIndex:index] imageDataArray; / / remove the sandbox in the array; NSArray *filePath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); / / get Document file path NSString *collectPath = filePath.lastObject; fileManager = [NSFileManager NSFileManager * defaultManager]; / / [remove all files FileManager removeItemAtPath:collectPath error:nil]; / / re write (int i = 0; for I < self.imageDataArray.count; i++) {NSData *imgData = self.imageDataArray[i] [self; WriteToBox:imgData] [self.collectionView reloadData];}};

And then there’s the “+” number. Here I used two CollectionViewcell, one to show the picture, one to show the “+” picture, and maybe there are other better ways, but I didn’t think of it…

- (NSInteger) collectionView: (UICollectionView *) collectionView numberOfItemsInSection: (NSInteger) section{return self.imageArray.count + 1;} - (UICollectionViewCell *) collectionView: (* UICollectionView) collectionView cellForItemAtIndexPath: (NSIndexPath *) indexPath{AddCollectionViewCell *cell1 [collectionView dequeueReusableCellWithReuseIdentifier:@ = "identifier" forIndexPath:indexPath]; if (self.imageArray.count = = 0) {return cell1;}else{CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@ "cell" forIndexPath:indexPath]; if (indexPath.item + 1 > self.imageArray.count) {return cell1;}else{cell.imageV.image = self.imageArra Y[indexPath.item]; [cell.imageV addSubview:cell.deleteButotn]; cell.deleteButotn.tag = indexPath.item + 100; [cell.deleteButotn addTarget:self action:@selector (deleteImage:) forControlEvents:UIControlEventTouchUpInside] return cell;};}}

The above is the main code, write to share with you all have shortcomings, the
TZImagePickerController Demo: write simple