objective c - CGImageDestinationFinalize save large gif the image become blur -
i want create gif asset, in osx10.12 right. in osx10.11, when record large image 5120 * 2880, , framecount larger 10, final gif become blur, , place white.
the cgimage asset right, after cgimagedestinationaddimage gif got wrong. guess maybe reason image large compress , memory cann't afford?
hope can help, help.
- (void)animatedgiffromvideoasset:(avasset *)asset timerange:(cmtimerange)timerange timeincrement:(float)increment destsize:(cgsize)destsize progress:(void(^)(float progress))convertprogress completion:(giffrommoviecompletion)completion { @autoreleasepool { dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ avassetimagegenerator * generator = [avassetimagegenerator assetimagegeneratorwithasset:asset]; float movielength = (float)timerange.duration.value / timerange.duration.timescale; int framecount = movielength / increment; float tolerance = 0.01f; nsstring *tempfile = [nstemporarydirectory() stringbyappendingpathcomponent:@"temp.gif"]; cfurlref fileurl = (__bridge cfurlref)[nsurl fileurlwithpath:tempfile]; cgimagedestinationref destination = cgimagedestinationcreatewithurl(fileurl, kuttypegif, framecount, nil); generator.requestedtimetolerancebefore = cmtimemakewithseconds(tolerance, 600); generator.requestedtimetoleranceafter = cmtimemakewithseconds(tolerance, 600); generator.appliespreferredtracktransform = yes; nsdictionary *frameproperties = [nsdictionary dictionarywithobject:[nsdictionary dictionarywithobject:[nsnumber numberwithfloat:increment] forkey:(nsstring *)kcgimagepropertygifdelaytime] forkey:(nsstring *)kcgimagepropertygifdictionary]; nsdictionary *gifproperties = [nsdictionary dictionarywithobject:[nsdictionary dictionarywithobject:[nsnumber numberwithint:0] forkey:(nsstring *)kcgimagepropertygifloopcount] forkey:(nsstring *)kcgimagepropertygifdictionary]; cgimagedestinationsetproperties(destination, (cfdictionaryref)gifproperties); // 压缩gif靠限制转换的最大gif宽度 if (!cgsizeequaltosize(nszerosize, destsize)) { generator.maximumsize = destsize; } nserror * error = nil; float beginseconds = cmtimegetseconds(timerange.start); (int = 0; < framecount; ++) { float progress = (i + 1) * 1.0f / framecount; convertprogress(progress); cmtime imagetime = cmtimemakewithseconds(beginseconds + * increment, 600); cgimageref image = [generator copycgimageattime:imagetime actualtime:null error:&error]; if (error) { if (destination) { cfrelease(destination); destination = null; } completion(nil, error); return; } cgimagedestinationaddimage(destination, image, (cfdictionaryref)frameproperties); if (image) { cfrelease(image); image = null; } usleep(10); } cgimagedestinationfinalize(destination); if (destination) { cfrelease(destination); destination = null; } nsdata *gifdata = [nsdata datawithcontentsoffile:tempfile]; completion(gifdata, error); }); }
}
Comments
Post a Comment