UIImage View + Pinch/Zoom feature when image is downloaded from web - iphone

I have a UIImageView, which pics up different images from the web, they are all different res. I have them to display as Aspect fit so that the whole picture displays even if it means leaving empty space on top or sides.
What i want to have the feature of doing is, once its displayed, to zoom in and out using the pinch or any other way.
NSURL* iURL = [NSURL URLWithString:tURL];
NSData* iData = [NSData dataWithContentsOfURL:iURL];
UIImage* iImage;
iImage = [UIImage imageWithData:iData];
FullScreenImage.image = iImage;
All this code is being executed on another thread, then it returns to the main thread and is displayed on a view using
[navigationController pushViewController:vFullscreen animated:YES];
Thanks for the help

You will need to use a UIScrollView as UIImageView does not offer any built-in zooming. See the Apple example entitled: ScrollViewSuite

Related

ScrollView with over 150 images crashes

I've an application which is a scrollView filled with over 150 images .. I've followed this tutorial to create it .. the application is over 550MBs and it has about 500 (150 for iPhone 5 & 150 for Retina & 150 for non-retina & buttons) photos .. The application runs very well on the simulator with no problems but on a real device when I open the application it keeps loading then a crash
so can anyone help me with this, please?
Thanks in advance.
Try to load only those images needed for displaying. ie while scrolling those images that are hidden should be taken off from the scrollview. Maybe you can make a design similar to the working of reusable UITableViewCell. ie a custom implementation for showing only the needed images and reusing them while scrolling.
There is another way, not a straight forward one, you can use a UITableView and add images to each cell and then rotate the tableview so that it will look like an horizontal scroll.
The advantage of doing this method is that the UITableView will handle all the reusability issue and we dont need to worry about. But I am not sure whether its the right way to do this.
Btw.. I have uploaded an app with the UITableView rotated horizontal scroll view to the appstore without getting rejected ;)
It is not an good way to do that ,u can upload your photo on server side and when open the app. loading the image from server.
id path = #"http://upload.wikimedia.org/wikipedia/commons/c/c7/Sholay-Main_Male_Cast.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
imageView.image = img;
LooK at this answer

Getting Blank Image while trying to capture MPMoviePlayerViewController view using UIGraphicsGetImageFromCurrentImageContext()

I have an requirement to capture iPhone screen when my app is in foreground . I have used UIGraphicsGetImageFromCurrentImageContaxt() for this it works in most of synerio but fails when video is playing by using MPMoviePlayerViewController or AVPlayer and gives back black image with player control.
Probabely My guess is MPMoviePlayerViewController rendering frames using OpenGl and method UIGraphicsGetImageFromCurrentImageContaxt() is not able to capture the image ??
I am missing something or is there any alternative soln available to capture iPhone Screen while app is in foreground ??
There is not any easy solution(i.e. might be I don't know) for this but you need to figure out. Here I have described possible solutions. When you will try simply rendering the view on context then it will give blank screen in place of player and other things will be as it is.
Possible solutions that I know
Private API
You can use UIGetScreenImage() function to capture whole screen of device including player and its control. This is the way you can get the image of player with view easily.
Note: Some buddies are saying use of this function may cause rejection of application(I have never used for app store's app so I don't have much experience about it :)).
Second way.
If you want to get image of player with the other things that resides in main view or iPhone screen then basic idea is to capture two images(i.e. One of movie player and another of whole screen like you are doing using UIGraphicsGetImageFromCurrentImageContext) and combine them as a one image using some proper calculations.In this way you need to do some calculations so accuracy is depend upon your calculations.
Here I am preferring to use AVPlayerinstead of MPMoviePlayer not sure I am true or not but as I have feel that AVPlayer providing exact frame at certain time(Good accuracy. Exact image that is showing on the screen.)
Please set the gravity mode of AVPlayer to AVLayerVideoGravityResizeAspect. This mode will preserver aspect ratio and fit within layers bounds(i.e. This option is default). You can set this way.
playerLayer.videoGravity = setVideoFillMode:AVLayerVideoGravityResizeAspect;
where playerLayer is the object of AVPlayerLayer.
Now get the image of AVPlayerLayer
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]
initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform=YES;
/*AVAssetImageGenerator will scale images such that they fit within the defined bounding box. Images will never be scaled up. So provide this should be the size of AVPlayer. */
imageGenerator.maximumSize=CGSizeMake(400, 400);
CGImageRef imgRef = [imageGenerator copyCGImageAtTime:storedCMTime actualTime:NULL error:NULL];
[imageGenerator release];
Please note that this generated image might be not exact size for what you are looking for or as you have exactly provided in maximum size because as I mentioned in above comment here image will be never scaled up. if image is not the exact size for what you are looking then use some image utility function to scale the image as per the size of your player but don't spoil Aspect ration because we have set the AVPLayer's mode to AVLayerVideoGravityResizeAspect.store this image temporary.
Now Capture the image of View using UIGraphicsGetImageFromCurrentImageContext(i.e. like you are doing currently). Draw image of AVplayer that we have captured and stored over exactly the black area that is showing on your view(i.e. You need to do some trial and error to get the exactly same).
Here I have tried to cover all the main points that may cause panic or not immediately obvious.
Update:
Screen capturing solutions from APPLE: http://developer.apple.com/library/ios/#qa/qa1703/_index.html#//apple_ref/doc/uid/DTS40010193
If you would like to go with MPMoviewPlayerController then it's okay. Get the thumbnail using code shows by #Kuldeep. And combine the Both view's image and Thumbnail using Image Masking exactly explain here : Link
I would rather suggest to use below snippet, may it would be more easy to use. You just need to pass time factor at which you need to capture the video.
float timeFactor = 60.0;
CGRect rect = CGRectMake(self.view.center.x, self.view.center.y, 100.0, 100.0)
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage *thumbnail = [player thumbnailImageAtTime:timeFactor timeOption:MPMovieTimeOptionNearestKeyFrame];
UIImageView *imagV=[[UIImageView alloc]initWithImage:thumbnail];
[imagV setFrame:rect];
Well, it seems I've found a solution to capture frames in a AVPlayer:
- (UIImage*) captureFrame {
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:[self.player.currentItem asset]];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5) {
[imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero];
[imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero];
}
CGImageRef ref = [imageGenerator copyCGImageAtTime:self.player.currentItem.currentTime actualTime:nil error:nil];
UIImage *img = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
return img;
}

Image Swiping In Objective-C

I'm just a beginner to iphone development.
Recently i'm doing a project in which images need to be swiped. Could any one help me with it?
The images are stored in a server whose link is given.
I'm need it badly. So please help me
thank you
There are two ways.
You could either look at using a Paging Scroll View
Or, You could look at getting a series of UIImageViews with the correct images in each and then setting up a UISwipeGestureRecogniser for both directions. On the swipe's event handler you can adjust the x and y positions of the imageViews.
This is basic stuff. You should read some of the How-to's on Apple's developer website to help you with the base knowledge.
Edit:
Regarding the Internet images, the code can be adapted into the paging scroll view example:
NSString *path = #"http://merrimusings.mu.nu/archives/images/groundhog2.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
Load the strings from an NSMutableArray instead and you're good to go!
Use Three20 photo Album. Your problem will be solved.

UIImage in uitableViewcell slowdowns scrolling table

I am loading image with data in my table view. Images are coming from web. I created method to get image url in model class.Model class has Nsdictionary and objects.
But this images is slowing down scrolling .Here is my code
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",
[(Tweet *)[recentTweets objectAtIndex:indexPath.row]urlString]]]]];
cell.imageView.image = image;
Please tell Where I am going wrong?
Use lazy loading and do your own drawing. Try to understand the techniques on the sample projects I linked. These are the best ways to improve the performance of tables with images.
here is the methodology I use for loading images into a UITableView from a remote location:
in your .h file, declare a mutable dictionary for storing the images:
NSMutableDictionary *images;
initialize the dictionary in -init... or in -viewDidLoad
images = [[NSMutableDictionary alloc]init];
in the .m, tableView:cellForRowAtIndexPath:, see if the image exists in your dictionary for the indexPath
UIImage *img = [images objectForKey: indexPath];
if the image does exist, just set it.
if (img) cell.imageView.image = img;
if the image does NOT exist, set the cell's image to a temporary image...
if (!img) cell.imageView.image = [UIImage imageNamed:#"imageUnavailable.png"];
AND add that image to your dictionary so it doesnt try to refetch the image if you scroll off and back to that image before it loads...
[images setObject:[UIImage imageNamed:#"imageUnvailable.png"] forKey: indexPath];
then, in this same block, use an NSOperationQueue, and a custom NSOperation ( here is a reference - NSOperation and SetImage) to get your image and call back into your UITableViewController with that image.
in your callback, add your image to the dictionary (overwriting the temp image) and call [tableView reloadData]
this will give you a nice non blocking user experience.
The are a couple of ways how to do it. I had the best experience with a Queue for the HttpRequests, which I pause during the scrolling process.
I highly recommend this framework for that:
http://allseeing-i.com/ASIHTTPRequest/
I also implemented an image cache, which only loads the images if there weren't in the cache.
And the last tweak was to actually draw the images instead of using a high level uicomponent like the UIImageView
The number one reason your code is slow right now is because you're making a network call on the main thread. This blocks an UI from being updated and prevents the OS from handling events (such as taps).
As already suggested, I recommend ASIHTTPRequest. Combine asynchronous requests with an NSOperationQueue with a smaller concurrency count (say, 2) for the image requests, caching, and reloading the rows when images come in (on the main thread) (also only reloading the row if its currently visible). You can get a smooth scrolling table view.
I have an example of this on github here: https://github.com/subdigital/iphonedevcon-boston
It's in the Effective Network Programming project and includes a set of projects that progressively improve the experience.
Download the images before you load the tableView, and store them in an NSArray. Then when the cellForRowAtIndexPath method is called, show a loading image until the image is in the array.

Memory Management

I need a clarification from all of you,That is I am implementing an iPad application. In that I tried to download and animate the images. The image count should be more than 100,000.The code I used to download and adding to the view is as follows.
UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,100,100)];
NSData *receivedData=nil;
receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://path/prudently/iphone/image_s/e545afbf-4e3e-442e-92f9-a7891fc3ea9f/test.png"]];
imageView.image = [[UIImage alloc] initWithData:receivedData] ;
[subView addSubview:imageView];
[imageView release];
But I am getting exception after I successfully added more than 8000 image to my subview. I am getting exception at getting data from the url. And one more thing I am not releasing the subview because once I downloaded them I need to animate the subview.
Please give me your suggessions
Thank you,
Sekhar Bethalam.
100,000 images would seem a lot for desktop applications, let alone a smart phone like an iPhone. Is there not another approach you can take to solve this problem that wouldn't need such a high resource count?
You can write the URL , images or something to a cached file, and divide some pages to animate the images...
When the user press a page link , application read and animate the images of this page, images of the page which user don't use need not display.
The only way you are going to accomplish this is to dynamically create and destroy the UIImageViews as they are needed on the screen. The iPhone/iPad/iAnything are incapable of doing what you want because of the limited memory available on the device.