IOS learning and ios learning routes

Source: Internet
Author: User
Tags file url

IOS learning and ios learning routes

1. Add categories to native UIImageView to support GIF playback

A GIF dynamic image file contains a set of images and information, which records the playback time of each frame, if we get all the images in the gif file and get the playback time of each frame, we can add a Key Frame Animation for the UIImageView to play the GIF content.

1. Create a category for UIImageView

2. parse the GIF to obtain the GIF information.

# Pragma mark -- parse DGIF-(void) gifWithUrl :( NSURL *) url returnData :( void (^) (NSArray <UIImage *> * imageArray, NSArray <NSNumber *> * timeArray, CGFloat totalTime, NSArray <NSNumber *> * widths, NSArray <NSNumber *> * heights )) dataBlock {// read the gif file as image data by referencing CGImageSourceRef source = CGImageSourceCreateWithURL (CFURLRef) url, NULL) through the File url ); // obtain the number of images in the gif file size_t count = CGImageSourceGetCount (source); // define a variable to record the time float allTime = 0 for a gif playback round; // store all images NSMutableArray * imageArray = [NSMutableArray array]; // store the playback time of each frame NSMutableArray * timeArray = [NSMutableArray array]; // store the width of each image (generally in a gif file, all files have the same size) NSMutableArray * widthArray = [NSMutableArray array]; // NSMutableArray * heightArray = [NSMutableArray array]; // traverse for (size_t I = 0; I <count; I ++) {// obtain the image information CGImageRef image = CGImageSourceCreateImageAtIndex (source, I, NULL); [imageArray addObject :( _ bridge UIImage *) (image)]; CGImageRelease (image ); // obtain the image information NSDictionary * info = (_ bridge NSDictionary *) CGImageSourceCopyPropertiesAtIndex (source, I, NULL); NSLog (@ "% @", info ); CGFloat width = [[info objectForKey :( _ bridge NSString *) kCGImagePropertyPixelWidth] floatValue]; CGFloat height = [[info objectForKey :( _ bridge NSString *) kCGImagePropertyPixelHeight] floatValue]; [widthArray addObject: [NSNumber numberWithFloat: width]; [heightArray addObject: [NSNumber numberWithFloat: height]; NSDictionary * timeDic = [info objectForKey :( _ bridge NSString *) kCGImagePropertyGIFDictionary]; CGFloat time = [[timeDic objectForKey :( _ bridge NSString *) interval] floatValue]; allTime + = time; [timeArray addObject: [NSNumber numberWithFloat: time];} dataBlock (imageArray, timeArray, allTime, widthArray, heightArray );}

3. Load GIF

TimingFunction: controls the animation running pace
  • KCAMediaTimingFunctionLinear (linear): constant speed, giving you a relatively static feeling
  • KCAMediaTimingFunctionEaseIn
  • KCAMediaTimingFunctionEaseOut: the animation enters at full speed and then slows down to the destination.
  • KCAMediaTimingFunctionEaseInEaseOut: the animation enters slowly, accelerates in the middle, and then slows down to the destination. This is the default animation action.
# Pragma mark -- load GIF-(void) cc_setImage :( NSURL *) imageUrl {_ weak id _ self = self; [self gifWithUrl: imageUrl returnData: ^ (NSArray <UIImage *> * imageArray, NSArray <NSNumber *> * timeArray, CGFloat totalTime, NSArray <NSNumber *> * widths, NSArray <NSNumber *> * heights) {CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath: @ "contents"]; NSMutableArray * times = [[NSMutableArray alloc] init]; float currentTime = 0; // set the time proportion of each frame for (int I = 0; I <imageArray. count; I ++) {[times addObject: [NSNumber numberWithFloat: currentTime/totalTime]; currentTime + = [timeArray [I] floatValue];} [animation setKeyTimes: times]; [animation setValues: imageArray]; [animation setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; // you can specify a circular animation. repeatCount = MAXFLOAT; // sets the total playback duration of the animation. duration = totalTime; // Add [[(UIImageView *) _ self Layer] addAnimation: animation forKey: @ "gifAnimation"] ;}] to the layer;}

 

Related Article

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.