IPhoneLowerUIScrollView image browserIs the content to be introduced in this article.ImageLibrary callImageIn this example, we plan to make a simpleImage browser.
The function is simple, fromImageLoad in LibraryImageAnd then put it under the view, and support zoom-in, zoom-out, and pan.
The first solution is as follows:
The preceding frameworks are the same:
Use toolbar as a button bar, which contains a button to trigger the slave button.
Press the action button to call the image library, and then send a message to the delegate image library. (the Protocol is attached to the main UIViewController, and the same is true for other delegate files ).
If the second parameter of the image UIImagePickerControllerDelegate imagePickerController: didFinishPickingMediaWithInfo: is obtained, a Dictionary containing the selected image is obtained.
OK. The show starts. The subsequent solution is as follows.
1. Set a UIImageView as the image carrier, fill the screen, set the Mode to Center, and display the image in the Center.
Then, use UIPinchGestureRecognizer to make a gesture. After the gesture is triggered, the response method is called back. Set the bounds and center of UIImageView in the method to ensure that they are displayed in the center. In the process, a type of extended UIImage is searched from the network, and the image can be scaled. Source code: (Note that the UIGraphicsXXXX function must be called in the main thread)
- //
- // UIImage_Extra.h
- // Camera
- //
- // Created by Li seleci on 11-4-22.
- // Copy 2011 _ MyCompanyName _. All rights reserved.
- //
- # Import <Foundation/Foundation. h>
- @ Interface UIImage (Extra)
- -(UIImage *) imageByScalingAndCroppingForSize: (CGSize) targetSize;
- @ End
- //
- // UIImage_Extra.m
- // Camera
- //
- // Created by Li seleci on 11-4-22.
- // Copy 2011 _ MyCompanyName _. All rights reserved.
- //
- # Import "UIImage_Extra.h"
- @ Implementation UIImage (Extra)
- -(UIImage *) imageByScalingAndCroppingForSize: (CGSize) targetSize
- {
- UIImage * sourceImage = self;
- UIImage * newImage = nil;
- CGSize imageSize = sourceImage. size;
- CGFloat width = imageSize. width;
- CGFloat height = imageSize. height;
- CGFloat targetWidth = targetSize. width;
- CGFloat targetHeight = targetSize. height;
- CGFloat scaleFactor = 0.0;
- CGFloat scaledWidth = targetWidth;
- CGFloat scaledHeight = targetHeight;
- CGPoint thumbnailPoint = CGPointMake (0.0, 0.0 );
- If (CGSizeEqualToSize (imageSize, targetSize) = NO)
- {
- CGFloat widthFactor = targetWidth/width;
- CGFloat heightFactor = targetHeight/height;
- If (widthFactor> heightFactor)
- ScaleFactor = widthFactor; // scale to fit height
- Else
- ScaleFactor = heightFactor; // scale to fit width
- ScaledWidth = width * scaleFactor;
- ScaledHeight = height * scaleFactor;
- // Center the image
- If (widthFactor> heightFactor)
- {
- ThumbnailPoint. y = (targetHeight-scaledHeight) * 0.5;
- }
- Else
- If (widthFactor
- {
- ThumbnailPoint. x = (targetWidth-scaledWidth) * 0.5;
- }
- }
- UIGraphicsBeginImageContext (targetSize); // this will crop
- CGRect thumbnailRect = CGRectZero;
- ThumbnailRect. origin = thumbnailPoint;
- ThumbnailRect. size. width = scaledWidth;
- ThumbnailRect. size. height = scaledHeight;
- [SourceImage drawInRect: thumbnailRect];
- NewImage = UIGraphicsGetImageFromCurrentImageContext ();
- If (newImage = nil)
- NSLog (@ "cocould not scale image ");
- // Pop the context to get back to the default
- UIGraphicsEndImageContext ();
- Return newImage;
- }
- @ End
A problem occurs when moving down the translation. Although touchesBegan and touchesMove can be reloaded to move UIImageView, iOS special effects are lost. When the image is moved to the edge of the screen, it is zoomed in and reduced, UIImageView does not necessarily fly there. You need to add judgment conditions. Therefore, this method is not advisable.
Now we have found a new api that can solve the stretching problem. There is a function in UIImage called stretchableImageWithLeftCapWidth: topCapHeight:. You can return a new UIImage, which can be stretched. For more information, see apple's Sample Code.
2. I checked on the Internet that I could use UIScrollView for translation processing, and embedded a UIImageView in it. The gesture was used to manage the enlarged and reduced images, so that I could eat hot pot while singing.
There are several things to consider in this solution. One is the contentSize of UIScrollView. The size of UIImageView should be taken into account. After the image is enlarged or reduced, the contentSize should be adjusted.
Another problem is that the center position of the scaling should be considered during scaling. For example, there is a personal face on the image, and the gesture is centered on the face and nose. After the enlargement, the center of the gesture may be biased, therefore, it is complicated to modify the UIImageView center during scaling.
3. To solve this problem, search for the document and find that there is zoom in UIScrollView. You can use the delegate viewForZoomingInScrollView method to specify a view in UIScrollView to zoom in, you can also set the maximum and minimum magnification. This nb, A UIScrollView class is all done with Pan scaling. Cool !!
But there are also issues to be aware. Because UIScrollView may have to actively call its subView attributes, do not modify the attributes of the subview During Scaling.
In addition, scaling and translation are all done by people, and you can change them in UIImageView.ImageYou must note thatUIScrollViewZoomScale, contentSize, and contentOffset of are all set to the initial values. Imagine a situation whereUIScrollViewAddImageZoom in, contentSize, contentOffset, and zoomScale are all changed. At this time, you have found a very smallImageIf you do not set the initial values of the three attributes, or set only one attribute, this will inevitably cause confusion.
The third of the three methods is the simplest.
Conclusion: I forgot to say in that book that Apple has been working on Mac for 20 years. If you need to use a lot of code to implement a very simple function, it must be a wrong method.
Today's exploration confirms this sentence. In addition, there are no principles in all iOS development books on the market. Therefore, the search engine, coupled with browsing various Website forums, has become a quick way to solve the problem.
Summary: Analysis onIPhoneImplementationScrollView image browserI hope this article will be helpful to you.