iOS development load, swipe to scroll through a large number of image optimization solutions

Source: Internet
Author: User

this article reprinted to http://mobile.51cto.com/iphone-413267.htmshare a private album today, read load, swipe through a large number of image solutions, I would like to emphasize that the concept of programming is not related to platform constraints. I would like to say in detail, after clicking on any small thumbnail in the thumbnail interface, into the high-definition Full Screen browsing interface of this short 1 seconds (and the next few seconds), what happened.

AD:2014WOT Global Software Technology Summit Beijing Station course video release

Share a private album today, read load, swipe through a large number of image solutions, I would like to emphasize that the concept of programming is not related to platform constraints.

I would like to say in detail, after clicking on any small thumbnail in the thumbnail interface, into the high-definition Full Screen browsing interface of this short 1 seconds (and the next few seconds), what happened.

General thinking Process

When you click on any small map,

1. First make ScrollView frame: size 2 ScrollView, small for gesture Zoom single picture, large horizontal load all photos sequentially

2. After making the ScrollView frame, load the photo

3. All ready jump page presented to the user selected large map

Loading the picture This step, if the photo album within 10 photos, then there is no technical challenge, but if it is 300 photos? A direct crash? or let the user wait for the load? Time-intensive task, this step needs to be split and optimized.

ScrollView Framework needs to understand the next API, and then move the brain, here is a little trick, a lot of people ask me how the black edge between photos and photos how to achieve, hehe, put the code:

  1. #define PADDING 20
  2. -(Nsinteger) Loadphotos
  3. {
  4. Photo before cleaning
  5. For (UIView *v in [_scrollview Subviews]) {
  6. [v Removefromsuperview];
  7. }
  8. Workingframe = [[UIScreen mainscreen]bounds];
  9. workingframe.origin.x = PADDING;
  10. for (int i = 0; i < int_total; i++) {
  11. CGRect frame = workingframe;
  12. Wqphoto *photoview = [[Wqphoto alloc] initwithframe:frame];
  13. [PhotoView setscroller:self];
  14. [PhotoView setindex:i];
  15. Wqalbumphoto *photo = [Albumobject._photos objectatindex:i];
  16. [Photo Cleanthumbnail];
  17. if (i = = int_current) {
  18. Loading the original
  19. [PhotoView SetImage:photo.oriImage];
  20. [PhotoView Setisload:yes];
  21. }else if (int_current-10 < i && i < Int_current + 10) {
  22. Load thumbnails near and around
  23. [PhotoView SetImage:photo.thumbnail4view];
  24. }
  25. [_scrollview Addsubview:photoview];
  26. workingframe.origin.x = workingframe.origin.x + 2 * PADDING
  27. + workingFrame.size.width;
  28. }
  29. Implement scrollable
  30. [_scrollview Setcontentsize:cgsizemake (Workingframe.origin.x, WORKINGFRAME.SIZE.HEIGHT/2)];
  31. [_scrollview setcontentoffset:cgpointmake (int_current, 0)];
  32. Load the remaining thumbnails
  33. Loadthread = [[Nsthread alloc]initwithtarget:self selector: @selector (loadimages) Object:nil];
  34. return 0;
  35. }
Using low-resolution graphs

Think carefully, in fact, there is no need to load all the pictures of the high-definition original, in advance to save each map supporting the low-resolution diagram, with space for time.

First load all the pictures of the low-resolution graph, when the user browses to a picture, the real-time loading of the original size HD uncensored large image. Process optimizations are:

multithreaded tasks

Even so, when the number of photos reached a certain amount, the time required to consume is not a millisecond level, experience can not be satisfied, page jumps will still appear in the phenomenon of Dayton.

Then consider using multithreading to further split the task, perform a jump and then perform the process of loading the low-resolution graph in the background.

Optimize your quick-flip experience

With this split, you can immediately jump and experience satisfaction. However, when browsing, when the user quickly swipe left and right, the chance of a black screen is very high, because the load of the low-resolution graph task may still be a lot of IO operation, not in time to keep up. In order to improve it, to load the low-resolution diagram of the steps to decompose again, excellence.
Within the allowable range of page jump time, load the high-definition artwork of the user-selected image, and load as many as possible a low-resolution picture of the neighboring pictures.

How do I load the rest of the low-resolution graphs?

To the user click on the selected map to the center of the load, the direct thinking should be two threads a left and right load, and then think about the loading can actually be a cycle, free of those who are the cost of the thread.

  1. -(BOOL) loadimages
  2. {
  3. for (int i = int_current-10, j = int_current + ten;!) (i < 0 && Int_total-1 < j);-I, ++j) {
  4. if (! ( I < 0)) {
  5. Wqphoto *photo_pre = [_scrollview.subviews objectatindex:i];
  6. Wqalbumphoto *photopre = [Albumobject._photos objectatindex:i];
  7. [Photo_pre SetImage:photoPre.thumbnail4view];
  8. }
  9. if (! ( Int_total-1 < J)) {
  10. Wqphoto *photo_next = [_scrollview.subviews objectatindex:j];
  11. Wqalbumphoto *photonext = [Albumobject._photos objectatindex:j];
  12. [Photo_next SetImage:photoNext.thumbnail4view];
  13. }
  14. }
  15. return YES;
  16. }
And finally a chop.

Try to reduce memory consumption. Because the original image is much larger than the low-resolution graph, when the user browses from one image to another, the current picture is loaded into a large, high-definition image of the original size, and the original is replaced with a low-resolution graph. Although the number of read and write increased, but memory does save a lot.

To tell you the truth, there are a lot of albums on the market, the app, will have a lag in the jumping feeling, hehe ...

iOS development load, swipe to scroll through a large number of image optimization solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.