Show multiple images in an Image View in IOS - ios

I'm using ELCImagePickerController in my project. Select multiple images from the gallery through it. I'm facing an issue that , how i can show the selected multiple images in an image view in a view controller. I have created a select image button which take us to photolibrary where we select multiple images.My code is,
- (IBAction)Select:(id)sender {
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
elcPicker.maximumImagesCount = 100; //Set the maximum number of images to select to 100
elcPicker.returnsOriginalImage = YES; //Only return the fullScreenImage, not the fullResolutionImage
elcPicker.returnsImage = YES; //Return UIimage if YES. If NO, only return asset location information
elcPicker.onOrder = YES; //For multiple image selection, display and return order of selected images
// elcPicker.mediaTypes = #[(NSString *)kUTTypeImage, (NSString
*)kUTTypeMovie]; //Supports image and movie types
elcPicker.imagePickerDelegate = self;
[self presentViewController:elcPicker animated:YES completion:nil];
}
- (void)elcImagePickerController:(ELCImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
for (NSDictionary *dict in info)
{
if ([dict objectForKey:UIImagePickerControllerMediaType] ==
ALAssetTypePhoto)
{
if ([dict objectForKey:UIImagePickerControllerOriginalImage])
{
UIImage* image=[dict
objectForKey:UIImagePickerControllerOriginalImage];
_arrimage.image;
}
}
}
}
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}

You can use CreolePhotoSelection to get and select multiple images. It will help to you gets multiple selection and retrieve all selected value into delegate method.
Below is the sample code for it:
CreolePhotoSelection *objCreolePhotoSelection= [[CreolePhotoSelection alloc] initWithNibName:#"CreolePhotoSelection" bundle:nil];
objCreolePhotoSelection.strTitle = YOUR_TITLE_FOR_PHOTO_VIEW;
objCreolePhotoSelection.delegate = self; // It will help to retrive all selected photos from CreolePhotoSelection
objCreolePhotoSelection.arySelectedPhoto = YOUR_CURRENT_ARRAY; // You need to pass same array which is holding selected image
objCreolePhotoSelection.maxCount = YOUR_MAX_PHOTOS_LIMIT; // Eg. 5, 10 anythings...
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:objCreolePhotoSelection];
[self.navigationController presentViewController:navController animated:YES completion:nil];
// Delegate Method
#pragma mark - Photo selected Delegate Method
-(void)getSelectedPhoto:(NSMutableArray *)aryPhoto
{
// You will selected image array into aryPhoto object and you can use it
}

First of all an Image View is to show a single image. From the piece of code you shared I am not clear what did you mean by
UIImage* image=[dict
objectForKey:UIImagePickerControllerOriginalImage];
_arrimage.image;
What does this _arrimage mean? Is that a reference to the Image view? then it should be
_arrimage.image = image; //the image fetched in the above line
But this won't fix your issue as this will show only the very last object found in the for loop (probably the last selected image).
To show all selected images it is better to go with a collection view with the array of images as the data source and load each images in each cells of the collection view.
If you want to show all images within the same image view, there is another option by setting it as animationimages
imageView.animationImages = imagesListArray //the list of image you selected
imageView.animationDuration = 2.0
imageView.startAnimating()
This just show each images one at a time from the array with a time interval of 2 seconds

You can't Show Multiple Images in a single ImageView at a same time but yes you can show multiple images on a same ImageView for sometime using Image Animation.
var imagesArray = [UIImage]()
for imageName in 1...3
{
imagesArray.append(UIImage(named: "\(imageName).png")!)
}
// You can also use below code to add images if not want to use loop
// imagesArray.append(UIImage(named: "a.png")!)
// imagesArray.append(UIImage(named: "b.png")!)
// imagesArray.append(UIImage(named: "c.png")!)
self.imageView.animationImages = imagesArray
self.imageView.animationDuration = 2.0
self.imageView.startAnimating()

Related

Extend the UIImageView to be a swipeable element that can display multiple photos

i have created a simple app with two buttons and one uiimageview.
One button takes a photo from photo library and puts it on uiimageview.
When the user adds another photo the old one should be saved and then user can swipe between these two photos.
another button takes a photo and put it on uiimageview.
So now i'm really confused about swiping between photos. I read that uiimageview can contain only one image. Also, i read that i need to add uiScrollView to UiImageView, but i don't know how they work together.
should i delete my UIImageView and then create ScrollView instead of it?
Here is my code
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)pickPhoto:(id)sender {
picker1 = [[UIImagePickerController alloc]init];
picker1.delegate = self;
[picker1 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker1 animated:YES completion:NULL];
}
- (IBAction)takePhoto:(id)sender {
picker2 = [[UIImagePickerController alloc]init];
picker2.delegate = self;
if([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
}
else{
NSLog(#"not avaialbe");
}
[self presentViewController:picker2 animated:YES completion:NULL];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
UPDATE
So i have added a method that allows user to swipe left or right and he can see different pictures, however, i had to hard code my images into the code. But i want to select images from the photo library, then these images will be saved in the array and then if user adds more pictures he has a choice to swipe through his old images.
Here is my code for swiping through the images
- (IBAction)Swipe:(id)sender {
NSArray *images = [[NSArray alloc]initWithObjects:#"ayat.png", #"qwe.png",#"4444", #"15", nil];
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch(direction)
{
case UISwipeGestureRecognizerDirectionLeft:
imageIndex++;
break;
case UISwipeGestureRecognizerDirectionRight:
imageIndex--;
break;
default:
break;
}
imageIndex = (imageIndex <0)?([images count] -1 ):
imageIndex % [images count];
self.imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}
but here i added pictures to my project.
It's true that a UIImageView can display only a single image.
There are various ways to handle this. One would be to use a UIPageViewController. That would enable you to set up side-swipe based paging between pages of content. There is a sample app from Apple called PhotoScroller (it used to be linked from the Xcode help system. With Xcode 6, apple seems to have removed all sample code. You'll have to search the net for the sample app. That app also includes tiled rendering, which is likely overkill for your application, but it does show how to set up a page view controller.
You could also use a UICollectionView, or create your own custom view that manages a set of views.
Actually now that I think about it a UICollectionView set up for paging might be your best bet.

UIImagePickerController Taking Picture From Camera - Default to square

Want to provide only square photo capture option from our app. Right now with the below code its giving square crop option after capturing. But want it to be exactly like default camera square option. Also we don't want to show anything other than square.
Below is the code we are using.
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
controller.allowsEditing = YES;
controller.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
// controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
Attached the screen shot
This is our app
We want it to be like this.
set allowEditing to YES. from the result dict use the key UIImagePickerControllerEditedImage
then you will have the squared image.
i find no way to let the user select, which format he want :(
as Shubham Narang has mention, use UIImagePickerControllerEditedImage in imagePickerController:didFinishPickingMediaWithInfo method.
UIImage *originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
if(!originalImage)
{
originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
}
This code will use original image if edited (cropped) image doesn't exist

Add photos to MWPhotoBrowser

I have my first view controller imageResults and in that calls the MWPhotoBrowser with the code below. I see it displays a new view controller using the data from self.photos. I've tried a few options for adding more data but none successful. I want to be able to load more data in the new view and add it to the photobrowser, the adding more data part is already don't, now how do i set the current browser to load the new array without loosing the current position?
-(void)mvTouched:(NSIndexPath *)indexPath {
NSLog(#"Arr:%lu Url:%lu", [titlesMutable count], [mediaUrlDict count]);
NSMutableArray *tmpPhotos = [NSMutableArray array];
for(NSString *titleString in titlesMutable) {
NSString *url = [mediaUrlDict objectForKey:titleString];
MWPhoto *currentPhoto = [MWPhoto photoWithURL:[NSURL URLWithString:url]];
currentPhoto.caption=[NSString stringWithFormat:#"%#", titleString];
[tmpPhotos addObject:currentPhoto];
//[tmpPhotos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:url]]];
}
self.photos = [NSArray arrayWithArray:tmpPhotos];
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set options
browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
browser.displayNavArrows = YES; // Whether to display left and right nav arrows on toolbar (defaults to NO)
browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
browser.alwaysShowControls = YES; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
browser.edgesForExtendedLayout = YES;
browser.extendedLayoutIncludesOpaqueBars = YES;
// Optionally set the current visible photo before displaying
//[browser setCurrentPhotoIndex:1];
[browser setCurrentPhotoIndex:indexPath.row];
self.navigationController.navigationBar.hidden=NO;
// Present
[self.navigationController pushViewController:browser animated:YES];
// Manipulate
[browser showNextPhotoAnimated:YES];
[browser showPreviousPhotoAnimated:YES];
//[browser setCurrentPhotoIndex:1];
[browser setCurrentPhotoIndex:indexPath.row];
[browser reloadData];
}
-(void)loadMore {
[self addDataToArray];
//[self mvTouched:nil];
[self.collectionView reloadData];
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _photos.count;
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index {
// Do your thing!
NSLog(#"ACTION!!!");
}
Calling [self.collectionView reloadData] will only reload the current view's collection instead of the MWPhotoBrowser's. You need to call the browsers reloadData function for this to work like so:
[browser reloadData]
Note that there is currently an issue with the browsers collection view not displaying the new photos after calling this method. I managed to work around this by adding this code to the reloadData method found in the MWPhotoBrowser.m file:
if (_gridController) {
[_gridController.collectionView reloadData];
}

uiimagepickercontrolleroriginalimage memory waring

I have a collection view (UICollectionViewController subclass) where I placed a UIBarButtonItem named "Take a photo". When I tap the bar button item my camera opens & I take a photo. In my project after "Use Photo" from camera it will move to a crop view controller where I crop the image after selecting the image from my crop view the image gets placed here in my collection view the current controller. In crop view I have two buttons use and cancel.My problem is when I take oddly 2,3 or 4 I receive a memory warning in console then the app crashes with a alert "application terminated due to memory pressure". I've been using this code, when I use UIImagePickerControllerEditedImage there is no issue instead if I use UIImagePickerControllerOriginalImage I come across memory issue.
My problem is to provide a good quality image and this UIImagePickerControllerOriginalImage provides a good quality than UIImagePickerControllerEditedImage.I have to get rid of this memory issue and so I used this line [self dismissViewControllerAnimated:YES completion:NULL];but I couldn't able clear the memory warning.
- (IBAction)TakeaPhoto:(id)sender {
[[UIApplication sharedApplication]setStatusBarHidden:FALSE withAnimation:NO];
gallery=0;
picker1 = [[UIImagePickerController alloc] init];
picker1.delegate = self;
self.resizeableCropArea =YES;
self.cropSize=CGSizeMake(296, 350);
picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker1 animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:
UIImagePickerControllerOriginalImage];
image_cap = [self imageTemp:image scaledToSize:CGSizeMake(320, 370)];
dataTemp = UIImageJPEGRepresentation(image,0.0);
CropViewController *cropController = [[CropViewController alloc] init];
cropController.sourceImage = [info objectForKey:UIImagePickerControllerOriginalImage];
Original_img = UIImageJPEGRepresentation(cropController.sourceImage,0.0);
[original_image addObject:[UIImage imageWithData:Original_img]];
NSLog(#"source image=%#",cropController.sourceImage);
cropController.resizeableCropArea = self.resizeableCropArea;
cropController.cropSize = self.cropSize;
cropController.delegate = self;
Cancel_Image= cropController.sourceImage;
[self dismissViewControllerAnimated:YES completion:NULL];
[self.navigationController
pushViewController:cropController animated:YES];
}
Maybe you should compress your JPEG images. For example if you need that your images have a size less than 70kb, you can use this code:
float compressionRate = 0.90; // Initial compression rate
float maxCompressionRate = 0.10; // Max compression rate
NSData *data = UIImageJPEGRepresentation(outputImage, compressionRate);
// Our limit of size is MAX_UPLOAD_SIZE (70000) and compressionRate is 0.03f
while ([data length] > MAX_UPLOAD_SIZE && compressionRate >= maxCompressionRate) {
compressionRate -= 0.03;
data = UIImageJPEGRepresentation(outputImage, compressionRate);
}
Note: you can edit MAX_UPLOAD_SIZE with you desired size in bytes.

Having trouble passing NSString from plist to ImageView

I'm currently using a plist to populate my table view rows(i.e. Name, Description and Image). When the user selects a row, a new controller is pushed up with an imageView presenting the rows image in full screen.
The problem that I'm facing is, passing the string to the new viewController's imageView.
All the NSLog's return the correct information, except that when it logs the UIImageView, it returns null then. Am I not connecting it correctly? The row doesn't display any image until it's selected (essentially the row is acting, similar to a thumbnail).
Any help would be greatly appreciated thank you!!!
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *childDictionary = [mainChildren objectAtIndex:indexPath.row];
//Image Name NSString from plist
childImage = [childDictionary objectForKey:#"Child Image"];
if ([childDictionary objectForKey:#"Child Image"] == nil) {
NSLog(#"No Image String Found.");
}
else {
NSLog(#"Image String Found. Image Name is: %#", childImage);
UIImage *myImage = [UIImage imageNamed:childImage];
NSLog(#"Image Found. Image is: %#", myImage);
UIImageView *childImageView = [childImageView setImage:myImage];
NSLog(#"ImageView Found. ImageView is: %#", childImageView);
FullscreenImageViewController *imgViewer = [[FullscreenImageViewController alloc] init];
imgViewer.fullScreenImageView = childImageView;
[self presentViewController:imgViewer animated:YES completion:nil];
}
}
When you instantiate a new UIImageView, it should be [[UIImageView alloc] initWithImage:image].
Or since your full screen image controller has property of image view, e.g. fullScreenImageView, you can just set the image of the property directly with a UIImage instance.

Resources