How to show Image Full Screen that showing in iCarousel in iOS? - ios

I am using iCarousel custom control to show image from web that consumed with JSON data.
Here is my codes to show image in iCarousel
to Load JSON Data in ViewDidLoad
JSONLoader *jsonLoader = [[JSONLoader alloc]init];
self.items = [[NSMutableArray alloc]init];
[self.items removeAllObjects];
self.items = (NSMutableArray *) [jsonLoader loadJSONDataFromURL:[NSURL URLWithString:#"https://public-api.wordpress.com/rest/v1/sites/www.myWebsite.com/posts?category=blog&page=1"]];
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(AsyncImageView *)view
{
MMSPLoader *mmObject = [self.items objectAtIndex:index];
view = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, 250.0f, 250.0f)];
view.layer.borderColor = [UIColor whiteColor].CGColor;
view.layer.borderWidth = 0.3f;
view.image=[UIImage imageNamed:#"page.png"];
view.imageURL = [NSURL URLWithString:[mmObject featureImageUrl]];
return view;
}
That can show image correctly. My case is when i tap on that image , i want to show that image in FULL SCREEN. So i used GGFullScreenImageViewController.
However when i tap on that Image to show FULL SCREEN , i retrieved Image URL and show in GGFullScreenImageViewController. It's fine but , i don't want to retrieve from that URL because it downloading image from web again and slowing to show.
In my idea , i saved that image when tap on image in iCarousel and show it in GGFullScreenImageViewController.
So i don't need to download image again.
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
dispatch_queue_t myqueue = dispatch_queue_create("com.i.longrunningfunctionMain", NULL);
dispatch_async(myqueue, ^{
UIApplication *apps = [UIApplication sharedApplication];
apps.networkActivityIndicatorVisible = YES;
MMLoader *mmObject = [self.items objectAtIndex:index];
NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mmObject.featureImageUrl]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = imageView;
dispatch_async(dispatch_get_main_queue(), ^{
apps.networkActivityIndicatorVisible = NO;
[self presentViewController:vc animated:YES completion:nil];
});
});
NSLog(#"%i",index);
}
So should i save to local file or is there any others nice idea?

Really you should use a library to save the image when you initially download it. AsyncImageView isn't necessarily the best choice as it just caches in memory.
That said, at the moment you can just get the image from the view. This isn't ideal, and you should save it to disk - just sooner rather than later. Look at, perhaps, SDWebImage for that.
To get the image from the view (typed in browser so verify syntax and API usage...):
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
AsyncImageView *view = (AsyncImageView *)[carousel itemViewAtIndex:index];
UIImageView *imageView = [[UIImageView alloc] initWithImage:view.image];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = imageView;
[self presentViewController:vc animated:YES completion:nil];
}

Related

i want show different images continuously without user touch the button

when the user touch the button the below code show the image.i want show different images like "bear,cat,cow,dog" continuously without user touch the button.can any one help me.
NSMutableArray *dashBoy = [NSMutableArray array];
for (i = 1; i<= 12; i++)
{
butterfly = [NSString stringWithFormat:#"bear_%d.png", i];
if ((image = [UIImage imageNamed:butterfly]))
[dashBoy addObject:image];
}
[stgImageView setAnimationImages:dashBoy];
[stgImageView setAnimationDuration:4.0f];
[stgImageView setAnimationRepeatCount:2];
[stgImageView startAnimating];
You could do as below:-
Firstly, create a UIImageView property like below,
#property (nonatomic,weak) IBOutlet UIImageView *imageView; //Also connect it with storyboard or xib.
Then you could write down IBAction as follow:-
- (IBAction)startTap:(id)sender {
[self.imageView setAnimationDuration:1.0f]; //You could change the duration for making animation fast or slow.
[self.imageView setAnimationImages:imageArray]; //imageArray is NSArray of images of which you want to show slide show.
[self.imageView startAnimating]; //simply to start animation.
}
- (IBAction)stopTap:(id)sender {
[self.imageView stopAnimating]; //To stop animation.
}
try this..
create two instance objects like
NSMutableArray * imagesArray; and UIImageView *animationImageView;
-(void)viewDidLoad
{
NSArray *imageNames = #[#"img1.png", #"img2.png", #"img3.png", #"img4.png",#"img5.png"];
imagesArray = [[NSMutableArray alloc]init];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++)
{
[imagesArray addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 193)];
animationImageView.animationImages = imagesArray;
animationImageView.animationDuration = 0.5; // set your own
[self.view addSubview:animationImageView];
}
and to start and stop two UIButton Actions
-(IBAction)startAnimation:(UIButton*)sender
{
[animationImageView startAnimating];
}
-(IBAction)stopAnimation:(UIButton*)sender
{
[animationImageView stopAnimating];
}

Controlling Load Ordering of UICollectionView cells with dispatch_async

In my iOS app, I have a UICollectionView where each cell contains an image. To prevent the view from taking a long time to load, I load each with a blank image and title before loading the image contents in a background task.
I logged which images are getting loaded through in the background async task, and it seems like the images of cells off screen get loaded first, followed by the cells at the top of the screen. This makes the app seem unresponsive, and I'd rather have the cells at the top take priority in terms of loading:
I also notice that once I start scrolling, the images in the cells suddenly start appearing, but they take much longer to appear on their own. Can anyone suggest strategies to control the ordering that UICollectionCells load in?
Here is my code:
Iterate over projects and add imageViews to an NSMutableArray projectContainers, which then gets turned into cells
for (NSDictionary *currentProject in projects)
{
// data entry
[projectIDs addObject: [currentProject objectForKey:#"id"]];
NSString *projectTitle = [currentProject objectForKey:#"title"];
id delegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = [delegate managedObjectContext];
CustomLabel *cellLabel=[[CustomLabel alloc]init];
cellLabel.text = trimmedProjectTitle;
[titles addObject:projectTitle];
CGSize maxLabelSize = CGSizeMake(cellWidth,100);
CustomLabel *titleLabel = [[CustomLabel alloc]init];
// titleLabel styling
titleLabel.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5f];
titleLabel.textColor =[UIColor whiteColor];
[titleLabel setFont: [UIFont fontWithName: #"HelveticaNeue" size:12]];
titleLabel.text = trimmedProjectTitle;
CGSize expectedLabelSize = [titleLabel.text sizeWithFont:titleLabel.font constrainedToSize:maxLabelSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect labelFrame = (CGRectMake(0, 0, cellWidth, 0));
labelFrame.origin.x = 0;
labelFrame.origin.y = screenWidth/2 - 80 - expectedLabelSize.height;
labelFrame.size.height = expectedLabelSize.height+10;
titleLabel.frame = labelFrame;
// add placeholder image with textlabel
UIImageView *imagePreview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cellWidth, cellHeight)];
imagePreview.contentMode= UIViewContentModeScaleAspectFill;
imagePreview.clipsToBounds = YES;
[imagePreview setImage:[UIImage imageNamed:#"blank.png"]];
[imagePreview addSubview:titleLabel];
[imagePreview.subviews[0] setClipsToBounds:YES];
[projectContainers addObject: imagePreview];
// add project thumbnail images in async
dispatch_async(bgQueue, ^{
NSDictionary *imagePath = [currentProject objectForKey:#"image_path"];
NSString *imageUrlString = [imagePath objectForKey: #"preview"];
NSURL *imageUrl = [NSURL URLWithString: imageUrlString];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:(imageUrl)];
UIImage *image = [[UIImage alloc] initWithData:(imageData)];
if(image){
NSLog(#"project with image: %#", projectTitle);
[imagePreview setImage: image];
}
BOOL *builtVal = [[currentProject objectForKey:#"built"]boolValue];
if(builtVal){
UIImageView *builtBanner =[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"built_icon.png"]];
builtBanner.frame = CGRectMake(screenWidth/2 -80, 0, 50, 50);
[imagePreview addSubview: builtBanner];
}
});
}
renders cells using the NSMutableArray projectContainers:
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// NSLog(#"cellForItemAtIndexPath");
static NSString *identifier = #"NewCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if(!reloadProjects){
UIImageView *preview = (UIImageView*) [cell.contentView viewWithTag:[[projectIDs objectAtIndex:indexPath.row]intValue]];
UIImageView *previewContent = [projectContainers objectAtIndex:indexPath.row];
// NSLog(#"fetching image tag %d", [[projectIDs objectAtIndex:indexPath.row]intValue]);
if (!preview)
{
previewContent.tag = [[projectIDs objectAtIndex:indexPath.row]intValue];
// NSLog(#"creating previewContent %li", (long) previewContent.tag);
[cell addSubview: previewContent];
}
[self.collectionView setBackgroundColor:collectionGrey];
cell.contentView.layer.backgroundColor = [UIColor whiteColor].CGColor;
return cell;
}
return cell;
}
EDIT: Working Solution
Thanks to rob mayoff for helping me come out with a solution. This is what I ended up doing, which loads the images much faster:
// add project thumbnail images in async
dispatch_async(imageQueue, ^{
NSDictionary *imagePath = [currentProject objectForKey:#"image_path"];
NSString *imageUrlString = [imagePath objectForKey: #"preview"];
NSURL *imageUrl = [NSURL URLWithString: imageUrlString];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:(imageUrl)];
UIImage *image = [[UIImage alloc] initWithData:(imageData)];
if(image){
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"project with image: %#", projectTitle);
[imagePreview setImage: image];
});
}
BOOL *builtVal = [[currentProject objectForKey:#"built"]boolValue];
if(builtVal){
UIImageView *builtBanner =[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"built_icon.png"]];
builtBanner.frame = CGRectMake(screenWidth/2 -80, 0, 50, 50);
dispatch_async(dispatch_get_main_queue(), ^{
[imagePreview addSubview: builtBanner];
});
}
});
There are several things that code be improved in your code, but your chief complaint (“once I start scrolling, the images in the cells suddenly start appearing, but they take much longer to appear on their own”) is because you violated the commandment:
Thou shalt only access
thy view hierarchy
from the main thread.
Look at your code:
dispatch_async(bgQueue, ^{
...
[imagePreview addSubview: builtBanner];
You're manipulating the view hierarchy from a background thread. This is not allowed. For example, see the note at the bottom of this page, or the “Threading Considerations” in the UIView Class Reference.
You need to dispatch back to the main thread to update the view hierarchy.
Watch the Session 211 - Building Concurrent User Interfaces on iOS video from WWDC 2012. It talks in depth about how to do what you're trying to do, efficiently. See also this answer.

Change image in UIImageView

i have two ViewControllers, when i tap a cell i need to open other viewController and see image (image with names 1, 2, 3,...._full.jpg), so i write:
DetailViewController *dvc = [[DetailViewController alloc] init];
[dvc updateImage:[NSString stringWithFormat:#"%d", indexPath.row]];
[self.navigationController pushViewController:dvc animated:YES];
in my dvc
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];
[self.image setTag:1];
[self.view addSubview:self.image];
}
method what i called
-(void)updateImage:(NSString*)imageName
{
[(UIImageView*)[self.view viewWithTag:1] setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#_full.jpg", imageName]]];
}
smth like self.image.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#_full.jpg", imageName]]; didn't work for me.
so, these code works fine in my IOS 5,6,7 emulators, but when i compile it on my IPad 2, IOS 5.1 my image is not updating, all i see is gray background.
What am i doing wrong?
You run
[dvc updateImage:[NSString stringWithFormat:#"%d", indexPath.row]];
After that viewDidLoad is called and your image is reseted.
Instead that pass image name as a string
[dvc setImageName:[NSString stringWithFormat:#"%d", indexPath.row]];
and in DetailViewController.m in viewDidLoad call
[self updateImage:[NSString stringWithFormat:#"%#_full.jpg", self.imageName]];
Remember to add #property imageName to your DetailViewController.h file.
omg, XCode is joking, it's all because of my images where not ".jpg" they where ".JPG"!. I'm angry...

iCarousel performance issue on iPad

I'm using Nick Lockwood's iCarousel for an iPad app. Right now it's just a carousel of 15 full screen images.
I setup a single view project, add the iCarousel view in storyboard, add it's view controller as a data source and put this code for the datasource methods:
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return 15;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
UIImage* img = [UIImage imageNamed:[NSString stringWithFormat:#"image%d.jpg", index]];
UIImageView* imgView = [[UIImageView alloc] initWithImage:img];
return imgView;
}
This works, but the first time I scroll through all the items you can notice a little performance hit when a new item is being added to the carousel. This does not happen the second time I go through all the items.
You can see what I mean in this profiler screenshot. The peaks in the first half are for the first time I scroll through all the images, then I do it again and there are no peaks and no performance hit.
How can I fix this?
EDIT
I isolated one of the peaks on instruments and this is the call tree
EDIT
The code with jcesar suggestion
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray* urls_aux = [[NSMutableArray alloc] init];
for (int i = 0; i<15; i++) {
NSString *urlPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"captura%d", i] ofType:#"jpg"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
[urls_aux addObject:url];
}
self.urls = urls_aux.copy;
self.carousel.dataSource = self;
self.carousel.type = iCarouselTypeLinear;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
if (view == nil) {
view = [[[AsyncImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)] autorelease];
view.contentMode = UIViewContentModeScaleAspectFit;
}
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:view];
((AsyncImageView *)view).imageURL = [self.urls objectAtIndex:index];
return view;
}
I don't think this is a very orthodox solution, but it works.
What I do is use iCarousel's method insertItemAtIndex:animated: to add the images one at a time on setup. Performance gets hit then, but after that everything runs smoothly.
- (void)viewDidLoad
{
[super viewDidLoad];
self.images = [[NSMutableArray alloc] init];
self.carousel.dataSource = self;
self.carousel.type = iCarouselTypeLinear;
for (int i = 0; i<15; i++) {
UIImage* img = [UIImage imageNamed:[NSString stringWithFormat:#"captura%d.jpg", i]];
[self.images addObject:img];
[self.carousel insertItemAtIndex:i animated:NO];
}
}
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return self.images.count;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
UIImage* img = [self.images objectAtIndex:index];
UIImageView* imgView = [[UIImageView alloc] initWithImage:img];
return imgView;
}
It's hard to be certain, but I'd guess that the first time through it's loading the images from the file; whereas the second time it's using the cached copies - that's something that +[UIImage imageNamed:] does automatically for you; without it, you'd see the performance hit every time. You could load the images earlier, either in your app delegate or in the initialiser for your controller.
Another thing you could do would be to make use of iCarousel's view reuse logic. I don't think it's responsible for your problem, but you may as well do it now:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
UIImage* img = [UIImage imageNamed:[NSString stringWithFormat:#"image%d.jpg", index]];
UIImageView* imgView = nil;
if ([view isKindOfClass:[UIImageView class]])
{
imgView = (UIImageView *)view;
imgView.image = img;
[imgView sizeToFit];
}
else
{
[[UIImageView alloc] initWithImage:img];
}
return imgView;
}
UPDATE: It would be great to get a bit more information on where your app is spending its time. If you use the Instruments Time Profiler you can get a breakdown of what percentage of the time is spent in each method.
Look at his example: Dynamic Downloads. He uses AsyncImageView instead UIImageView, that loads the images in an async way. So it won't try to load all the 15 images at the same time, and I think it shouldn't have performance issues

MFMailComposeViewController:Attach Photos (multi photos)

i want attach multi photos to one mail for my application
with this code i can attach only the last one photos to mail but i can read all photos in uiimageview
how can attaching all photos to one mail ?
here is the codes for read image
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {
[self dismissModalViewControllerAnimated:YES];
////
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController * mailControler = [[MFMailComposeViewController alloc]init];
mailControler.mailComposeDelegate = self;
mailControler.modalPresentationStyle = UIModalPresentationFormSheet;
NSString *emailBody = #""; // optional
[mailControler setMessageBody:emailBody isHTML:YES];
for (UIView *v in [scrollview subviews]) {
[v removeFromSuperview];
}
CGRect workingFrame = scrollview.frame;
workingFrame.origin.x = 0;
for(NSDictionary *dict in info) {
imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[scrollview addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
NSLog(#"image %#", imageview.image);
NSData * data = UIImageJPEGRepresentation(imageview.image, 0.0);
[mailControler addAttachmentData:data mimeType:#"image/jpeg" fileName:#"Photos"];
}
[scrollview setPagingEnabled:YES];
[scrollview setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}
Part 2 go mail
-(IBAction)actionEmailComposer
{
[self presentViewController:mailControler animated:YES completion:nil];
}
the app is crashed
You've got a whole mess of code in there about obtaining your images and dealing with your UI that is irrelevant to the question (and quite possibly why your app is crashing -- doesn't look like it's crashing because of your MFMailComposeViewController interaction). How you're obtaining your images is very hard to follow without the context of your larger UI.
But to focus on just your core question: how do you attach multiple photos to one email?
Answer: call [mailControler addAttachmentData: mimeType: fileName: multiple times. You can call it as many times as you need, provided you don't send two items with the same filename.

Resources