开发者

UIImage stutter on first load

开发者 https://www.devze.com 2023-02-24 09:58 出处:网络
I have a UIScrollview that zooms a PNG image in and out on a double开发者_如何转开发 tap. The way I have it set up, I create a few multiple sizes of the PNG image using UIGraphicsBeginImageContext/UIG

I have a UIScrollview that zooms a PNG image in and out on a double开发者_如何转开发 tap. The way I have it set up, I create a few multiple sizes of the PNG image using UIGraphicsBeginImageContext/UIGraphicsEndImageContext and store all the UIImages in an NSMutableArray. I then show the correct image on screen by swapping a UIImageView's image to the correct UIImage based on the current zoom level (I do all this to show a nicely anti-aliased image at all times rather than scaling just the original image).

The problem I have is that the very first time the image gets swapped to one that hasn't been displayed before, there is a slight stutter. After the first time, I can zoom in and out all day and there is no stutter. I have tried the solutions suggested here and here but they did not fix the problem.

Currently, I found a workaround by swapping the image after 0.01 seconds, and canceling any pending swap requests in the meantime. This works ok but it's not a solid fix. Obviously there has to be a way to get the images in a ready state since they become ready after the first time they are displayed. Please help me!


You don't actually need to create different sizes of the png. What you should have is a UIImageView inside the scrollview with the original PNG as it's image. Then add this to your .m file (make sure you have in your header file.

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return theImageView; }

you can also set the max and minimum zoom scale by doing this:

[scrollview setMinimumZoomScale:0.5];//will be half size
[scrollview setMaximumZoomScale:3.0];//will be 3X

Doing the above will automatically set up the pinch zoom for you. THen you just need to code in to listen for the double tap and tell it to do this:

[scrollview setZoomScale:1.0 animated:YES];//returns it back to original size

Hope this helps - let me know if not the effect you were looking for.

Cheers,

Michael

0

精彩评论

暂无评论...
验证码 换一张
取 消