MWPhotoBrowser icon indicating copy to clipboard operation
MWPhotoBrowser copied to clipboard

How to put a delete button along with share button like native Photos app

Open RajChanchal opened this issue 9 years ago • 10 comments

Hi, How can I make the MWPhotoBrowser to look like native Photos app browser. I wanted to put the delete and share button at bottom, I don't need 'Edit' button though. Is there any way or code you might already have implemented? Or can you help implementing it?

RajChanchal avatar Feb 17 '15 09:02 RajChanchal

STOP SPAMMING THIS TOOL...

This tool is for showing images as gallery ONLY.

What you want is can be done, but you have to put efforts.

What you want is TOTALLY additional FEATURES.

fparkar avatar Feb 17 '15 09:02 fparkar

i solve it . by add a delegate.

KKKKaras avatar Jul 28 '15 09:07 KKKKaras

@KKKKaras , I stopped working on the tool for a moment. Could you share the code, it will be helpful in future or send pull request.

RajChanchal avatar Jul 28 '15 10:07 RajChanchal

@RajChanchal ok~ im a novice.

I add the method at the MWPhotoBrowserDelegate ( in the MWPhotoBrowser.h)

  • (void)deleteAnyPicture;

then add this (MWPhotoBrowser.m) [_compileBtn addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_compileBtn];

#pragma mark -补充删除- -(void)delete:(id)sender { // user delegate [self.delegate deleteAnyPicture]; } ----------------------------when use -------------------------

  • (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {

    //if no photo popViewControllerAnimated if(_mwPhotos.count == 0) {

    [self.navigationController popViewControllerAnimated:YES];
    

    } return _mwPhotos.count; }

  • (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index { if (index < _imgData.count) return [_mwPhotos objectAtIndex:index]; return nil; }

  • (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index { if (index < _imgData.count) return [_mwPhotos objectAtIndex:index]; return nil; }

  • (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index { NSLog(@"Did start viewing photo at index %lu", (unsigned long)index);

_imgIndex=(unsigned long)index;// GET index }

-(void)deleteAnyPicture// delegate call this method { NSLog(@"you delete the %d photo",_imgIndex); [_mwPhotos removeObjectAtIndex:_imgIndex];

NSLog(@"%@",_imgData[_imgIndex]);
//判断删除了哪一张图片
[_ImgAndVedioData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if([obj objectForKey:@"fullImage"]==_imgData[_imgIndex])
    {
        [_ImgAndVedioData removeObject:obj];
    }
}];
[browser reloadData];

} // the _imgIndex is int 。record photo‘s index that you choose。 my English is poor 。im sorry :(

KKKKaras avatar Jul 28 '15 10:07 KKKKaras

Thanks, I hope it will be helpful in future for anyone wanting this feature.

RajChanchal avatar Jul 28 '15 10:07 RajChanchal

@KKKKaras , For me, it was simple. I just used the already developed delegate method:

  • (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index; Because I didn't need a 'share' button. I just commented code in - (void)actionButtonPressed:(id)sender method in 'MWPhotoBrowser.m' file except the following code:

    // Only react when image has loaded id <MWPhoto> photo = [self photoAtIndex:_currentPageIndex]; if ([self numberOfPhotos] > 0 && [photo underlyingImage]) {

    // If they have defined a delegate method then just message them
    if ([self.delegate respondsToSelector:@selector(photoBrowser:actionButtonPressedForPhotoAtIndex:)]) {
    
        // Let delegate handle things
        [self.delegate photoBrowser:self actionButtonPressedForPhotoAtIndex:_currentPageIndex];
    
    }
    

    }

And yes, to show a 'trash' bin instead of 'share' button in bottom bar just change following line in MWPhotoBrowser.m file: _actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionButtonPressed:)];

and make it:

_actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(actionButtonPressed:)];

Then in the delegate class implement the actionButtonPressed method and delete the image from your array at the received index and call reloadData method of the browser.

RajChanchal avatar Aug 10 '15 12:08 RajChanchal

@RajChanchal yes good job!~~

KKKKaras avatar Sep 21 '15 06:09 KKKKaras

Heres my version in swift:

https://github.com/mwaterfall/MWPhotoBrowser/issues/239#issuecomment-152324938

Esqarrouth avatar Oct 29 '15 21:10 Esqarrouth

Can anybody make example with this ? so, Other user can implement it in their project. Because delete is also one of the main functionality For such type of awesome control.

badalpub1991 avatar Dec 02 '15 07:12 badalpub1991

Here's another way to change the appearance of the action button, without subclassing or making changes in MVPhotoBrowser. In case anybody finds it useful.

MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];

// Set options ...

// Present on navigation controller
[navigationController pushViewController:navigationController animated:YES];

// Replace the action button with a trash button
UIToolbar* toolbar = (UIToolbar*)[browser.view subviewOfClass:[UIToolbar class]];
UIBarButtonItem* originalActionButton = toolbar.items.lastObject;
UIBarButtonItem* newActionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:originalActionButton.target action:originalActionButton.action];
NSMutableArray* items = [toolbar.items mutableCopy];
[items removeObject:originalActionButton];
[items addObject:newActionButton];
toolbar.items = [items copy];

Note that subviewOfClass: is just a method in a UIView extension that scans through the tree of subviews in a view looking for the first instance of a particular class.

A little hackish, but works well.

dblapps avatar Jul 03 '17 02:07 dblapps