SVProgress HUD is a lightweight loading display control, and the simple and easy-to-use API is very popular with developers. Are you tired of the classic turn-around animation? Here's a way to show our own gif animation.
Loading custom GIF
SVProgressHUD provides an api for displaying custom pictures, but does not support gif format. It still displays a static picture directly by using the following method
[SVProgressHUD showImage:[UIImage imageNamed:@"loading.gif"] status:@"Loading..."];
Creating Dynamic Image
+ (UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration
This class of UIImage methods can create a dynamic Image using a set of images. Here we need to first get an array of images consisting of a frame Image from a gif graph, and then get the dynamic Image we want. Code repository In Demo, UIImage's classified UIImage+GIFImage encapsulates this method and can be used directly. The following is one of the implementation processes:
+ (UIImage *)imageWithGIFData:(NSData *)data { if (!data) return nil; CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); size_t count = CGImageSourceGetCount(source); UIImage *animatedImage; if (count <= 1) { animatedImage = [[UIImage alloc] initWithData:data]; } else { NSMutableArray *images = [NSMutableArray array]; NSTimeInterval duration = 0.0f; for (size_t i = 0; i < count; i++) { // Take out every frame of Gif CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL); //Learning... Set the animation time to calculate the display time of each frame (frame time) NSTimeInterval frameDuration = [UIImage sd_frameDurationAtIndex:i source:source]; duration += frameDuration; // Add each frame to the array [images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]]; // Release real picture objects CFRelease(image); } // Setting the animation duration if (!duration) { duration = (1.0f / 10.0f) * count; } animatedImage = [UIImage animatedImageWithImages:images duration:duration]; } // Release Source Gif Pictures CFRelease(source); return animatedImage; }
Then we can call its class method to show the custom gif animation.
[SVProgressHUD setInfoImage:[UIImage imageWithGIFNamed:@"loading"]]; [SVProgressHUD showInfoWithStatus:@"Loading..."];
But this gif icon seems smaller than we thought...
Modify the size of the picture
Looking back at the source code of SVProgress HUD, imageView is fixed size (28x28). We also found that imageView is a private property in. m file, and there is no way to set the image frame in. h file. And although SVProgressHUD uses singletons, it does not expose the method of obtaining singletons sharedView in. h, so we need to create conditions to obtain singletons of SVProgressHUD, and then find ways to modify the attribute size of imageView.
- Create a new extension of SVProgressHUD, SVProgressHUD_Extension.h, to create a forward reference to the sharedView method
-
Get the private property imageView of the object by KVC and modify the size
UIImageView *svImgView = [[SVProgressHUD sharedView] valueForKey:@"imageView"]; CGRect imgFrame = svImgView.frame; // Set the display size of the picture imgFrame.size = CGSizeMake(64, 48); svImgView.frame = imgFrame;
Easy to use
Configure SVProgress HUD picture gif and animation time in AppDelegate.m, and use SVProgress HUD show InfoWithStatus:@ directly in the controller. "]; OK!
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self svPreferrenceConf]; return YES; } #Pragma mark - - SV Progress HUD preference setting -(void)svPreferrenceConf { [SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight]; [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack]; [SVProgressHUD setBackgroundColor:[UIColor whiteColor]]; [SVProgressHUD setMinimumDismissTimeInterval:CGFLOAT_MAX]; [SVProgressHUD setInfoImage:[UIImage imageWithGIFNamed:@"loading"]]; UIImageView *svImgView = [[SVProgressHUD sharedView] valueForKey:@"imageView"]; CGRect imgFrame = svImgView.frame; // Set the display size of the picture imgFrame.size = CGSizeMake(64, 48); svImgView.frame = imgFrame; }