iOS制作彈幕表情 文字圖片

gif+文字

+ (NSArray*)addWordToGifWithGifData:(NSData*)imageData withLabel:(UILabel*)label superView:(UIView*)superV{

CGImageSourceRef gifSource = CGImageSourceCreateWithData(CFBridgingRetain(imageData), NULL);/**


? ? size_tgifCount =CGImageSourceGetCount(gifSource);


? ? NSMutableArray *frames = [[NSMutableArray alloc]init];


? ? for(size_ti =0; i< gifCount; i++) {


? ? ? ? CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);


? ? ? ? UIImage*image = [UIImageimageWithCGImage:imageRef];

? ? ? ? UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, superV.frame.size.width, superV.frame.size.height)];

? ? ? ? imageView.image= image;

? ? ? ? NSData *tempArchive = [NSKeyedArchiver archivedDataWithRootObject:label];

? ? ? ? UILabel *textLabel = (UILabel *)[NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];

? ? ? ? [imageViewaddSubview:textLabel];

? ? ? ? UIGraphicsBeginImageContextWithOptions(superV.frame.size, NO, 0.0);//清晰

? ? ? ? [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];

? ? ? ? UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();

? ? ? ? UIGraphicsEndImageContext();

? ? ? ? [framesaddObject:aimg];


? ? ? ? CGImageRelease(imageRef);


? ? }

?returnframes;

}

+ (NSString*)creatGifWithImageArrar:(NSArray*)imageArrayM

{

//獲取源數(shù)據(jù)image

? ? NSMutableArray *imgs = [[NSMutableArray alloc]initWithArray:imageArrayM];


? ? //圖像目標

? ? CGImageDestinationRef destination;


? ? //創(chuàng)建輸出路徑

? ? NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

? ? NSString*documentStr = [documentobjectAtIndex:0];

? ? NSFileManager *fileManager = [NSFileManager defaultManager];

? ? NSString *textDirectory = [documentStr stringByAppendingPathComponent:@"gif"];

? ? [fileManagercreateDirectoryAtPath:textDirectory withIntermediateDirectories:YES attributes:nil error:nil];

? ? NSString *path = [textDirectory stringByAppendingPathComponent:@"test101.gif"];


? ? //創(chuàng)建CFURL對象

? ? /*

?? ? CFURLCreateWithFileSystemPath(CFAllocatorRef allocator, CFStringRef filePath, CFURLPathStyle pathStyle, Boolean isDirectory)


?? ? allocator : 分配器,通常使用kCFAllocatorDefault

?? ? filePath : 路徑

?? ? pathStyle : 路徑風(fēng)格,我們就填寫kCFURLPOSIXPathStyle 更多請打問號自己進去幫助看

?? ? isDirectory : 一個布爾值,用于指定是否filePath被當作一個目錄路徑解決時相對路徑組件

?? ? */

? ? CFURLRef url = CFURLCreateWithFileSystemPath (

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? kCFAllocatorDefault,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (CFStringRef)path,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? kCFURLPOSIXPathStyle,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? false);


? ? //通過一個url返回圖像目標 kUTTypeGIF? CFStringRef


? ? destination =CGImageDestinationCreateWithURL(url, kUTTypeGIF, imageArrayM.count, NULL);


? ? //設(shè)置gif的信息,播放間隔時間,基本數(shù)據(jù),和delay時間

? ? NSDictionary*frameProperties = [NSDictionary

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.001f], (NSString *)kCGImagePropertyGIFDelayTime, nil]

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:(NSString*)kCGImagePropertyGIFDictionary];


? ? //設(shè)置gif信息

? ? NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];

? ? //

? ? [dictsetObject:[NSNumber numberWithBool:YES] forKey:(NSString*)kCGImagePropertyGIFHasGlobalColorMap];


? ? [dictsetObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];


? ? [dictsetObject:[NSNumber numberWithInt:8] forKey:(NSString*)kCGImagePropertyDepth];


? ? [dictsetObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];

? ? NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:dict

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:(NSString*)kCGImagePropertyGIFDictionary];

? ? //合成gif

? ? for(UIImage* dImginimageArrayM)

? ? {

? ? ? ? CGImageDestinationAddImage(destination, dImg.CGImage, (__bridgeCFDictionaryRef)frameProperties);

? ? }

? ? CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);

? ? CGImageDestinationFinalize(destination);

? ? CFRelease(destination);


? ? returnpath;

}

?圖片合成文字

?@param text? ? ? ? ? ? 文字

?@param fontSize? ? ? ? 字體大小

?@param textColor? ? ? 字體顏色

?@param textFrame? ? ? 字體位置

?@param image? ? ? ? ? 原始圖片

?@param viewFrame? ? ? 圖片所在View的位置

?@return UIImage *

?*/

+ (UIImage*)imageWithText:(NSString*)text

? ? ? ? ? ? ? ? ? textFont:(NSInteger)fontSize

?? ? ? ? ? ? ? ? textColor:(UIColor*)textColor

?? ? ? ? ? ? ? ? textFrame:(CGRect)textFrame

?? ? ? ? ? ? ? originImage:(UIImage*)image

? ? imageLocationViewFrame:(CGRect)viewFrame

?? ? ? ? ? ? ? ? ? ? label:(UIImageView*)label;


//////////////////////////////////////圖片合成文字//////////////////////////////////////////////

+ (UIImage*)imageWithText:(NSString*)text

? ? ? ? ? ? ? ? ? textFont:(NSInteger)fontSize

?? ? ? ? ? ? ? ? textColor:(UIColor*)textColor

?? ? ? ? ? ? ? ? textFrame:(CGRect)textFrame

?? ? ? ? ? ? ? originImage:(UIImage*)image

? ? imageLocationViewFrame:(CGRect)viewFrame

?? ? ? ? ? ? ? ? ? ? label:(UIImageView*)label{?


? ? if(!text)? ? ? {? returnimage;? }

? ? if(!fontSize)? {? fontSize =17;? }

? ? if(!textColor) {? textColor = [UIColorblackColor];? }

? ? if(!image)? ? {? returnnil;? }

? ? if(viewFrame.size.height==0|| viewFrame.size.width==0|| textFrame.size.width==0|| textFrame.size.height==0){returnnil;}

? ? NSString*mark = text;

? ? CGFloatheight = [marksizeWithPreferWidth:textFrame.size.widthfont:[UIFontsystemFontOfSize:fontSize]].height;// 此分類方法要導(dǎo)入頭文件

? ? if((height + textFrame.origin.y) > viewFrame.size.height) {// 文字高度超出父視圖的寬度

? ? ? ? height = viewFrame.size.height- textFrame.origin.y;

? ? }

? ? UIImageView *testImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewFrame.size.width, viewFrame.size.height)];

? ? testImageV.image= image;

? ? for(inti =0; i < label.subviews.count; i ++) {

? ? ? ? if (![label.subviews[i] isKindOfClass:[UIImageView class]]) {

? ? ? ? ? ? NSData *tempArchive = [NSKeyedArchiver archivedDataWithRootObject:label.subviews[i]];

? ? ? ? ? ? UILabel *textLabel = (UILabel *)[NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];

? ? ? ? ? ? [testImageVaddSubview:textLabel];

? ? ? ? }

? ? }

//? ? CGFloat w = image.size.width;

//? ? CGFloat h = image.size.height;

//? ? UIGraphicsBeginImageContext(viewFrame.size);//模糊

//? ? [image drawInRect:CGRectMake(0, 0, viewFrame.size.width, viewFrame.size.height)];

//? ? NSDictionary *attr = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize], NSForegroundColorAttributeName : textColor };


? ? //位置顯示

//? ? [mark drawInRect:CGRectMake(textFrame.origin.x, textFrame.origin.y, textFrame.size.width, height) withAttributes:attr];

//? ? [label drawTextInRect:textFrame];

? ? UIGraphicsBeginImageContextWithOptions(viewFrame.size, NO, 0.0);//清晰

? ? [testImageV.layer renderInContext:UIGraphicsGetCurrentContext()];

? ? UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();

? ? UIGraphicsEndImageContext();

? ? returnaimg;

}

彈幕表情

-(void)makeGif {

? ? [MBProgressHUDshowMessag:@"正在生成"toView:self.view];

? ? __blockintflagkkk =1;

? ? NSMutableArray *arrpoiu = [NSMutableArray arrayWithCapacity:0];

? ? NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {

? ? ? ? //? ? ? ? 將定時器時間內(nèi)每隔.1(截圖頻率,自己可以控制)的屏幕截圖儲存起來,保存成功后進行下一張存放

? ? ? ? flagkkk +=1;

? ? ? ? if(flagkkk ==10) {

? ? ? ? ? ? [timerinvalidate];

? ? ? ? ? ? timer =nil;

? ? ? ? ? ? flagkkk =1;

? ? ? ? ? ? [selfcreategif:arrpoiu];

? ? ? ? ? ? NSData *data1lll = [NSData dataWithContentsOfFile:_pathUrl];

? ? ? ? ? ? //? ? ? ? 保存到本地相冊

? ? ? ? ? ? dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

? ? ? ? ? ? ? ? NSData*datapooo = data1lll;

? ? ? ? ? ? ? ? [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

? ? ? ? ? ? ? ? ? ? [[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:datapooo options:nil];

? ? ? ? ? ? ? ? }completionHandler:^(BOOLsuccess,NSError*_Nullableerror) {

? ? ? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{

? ? ? ? ? ? ? ? ? ? ? ? [MBProgressHUD hideHUDForView:self.view animated:YES];

? ? ? ? ? ? ? ? ? ? ? ? if(success) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? [MBProgressHUDshowSuccess:@"保存成功"toView:self.view];


? ? ? ? ? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? ? ? ? ? ? ? [MBProgressHUDshowError:@"保存失敗,請重試"toView:self.view];

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? });

? ? ? ? ? ? ? ? }];

? ? ? ? ? ? });

? ? ? ? }

? ? ? ? UIImage *imageppoj = [screenShotToll shotInView:self.tscroitleLabel];

? ? ? ? [arrpoiuaddObject:imageppoj];

? ? }];

? ? [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

}


-(void)creategif:(NSArray*)arr

{


? ? //圖像目標

? ? CGImageDestinationRef destination;

? ? NSString*path ;

? ? //創(chuàng)建輸出路徑

? ? NSFileManager *fileManager = [NSFileManager defaultManager];


? ? NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

? ? NSString*documentStr = [documentobjectAtIndex:0];

? ? NSString *textDirectory = [documentStr stringByAppendingPathComponent:@"gif"];



? ? [fileManagercreateDirectoryAtPath:textDirectory withIntermediateDirectories:YES attributes:nil error:nil];

? ? NSString *gifName = [NSString stringWithFormat:@"yangyang.gif"];

? ? path = [textDirectorystringByAppendingPathComponent:gifName];


? ? CFURLRef url = CFURLCreateWithFileSystemPath (

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? kCFAllocatorDefault,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (CFStringRef)path,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? kCFURLPOSIXPathStyle,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? false);


? ? //通過一個url返回圖像目標

? ? destination =CGImageDestinationCreateWithURL(url, kUTTypeGIF, arr.count, NULL);

? ? //設(shè)置gif的信息,播放間隔時間,基本數(shù)據(jù),和delay時間

? ? NSDictionary*frameProperties = [NSDictionary

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3], (NSString *)kCGImagePropertyGIFDelayTime, nil]

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:(NSString*)kCGImagePropertyGIFDictionary];


? ? //設(shè)置gif信息

? ? NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];


? ? [dictsetObject:[NSNumber numberWithBool:YES] forKey:(NSString*)kCGImagePropertyGIFHasGlobalColorMap];


? ? [dictsetObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];


? ? [dictsetObject:[NSNumber numberWithInt:8] forKey:(NSString*)kCGImagePropertyDepth];


? ? [dictsetObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];

? ? NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:dict

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? forKey:(NSString*)kCGImagePropertyGIFDictionary];

? ? //合成gif

? ? for(UIImage* dImginarr)

? ? {

? ? ? ? CGImageDestinationAddImage(destination, dImg.CGImage, (__bridgeCFDictionaryRef)frameProperties);

? ? }

? ? CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);

? ? CGImageDestinationFinalize(destination);

? ? CFRelease(destination);


? ? _pathUrl= path;


}


+ (UIImage*)shotInView:(UIView*)view

{

? ? UIGraphicsBeginImageContext(view.bounds.size);

? ? [view.layer renderInContext:UIGraphicsGetCurrentContext()];

? ? UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

? ? UIGraphicsEndImageContext();

? ? returnimage;

}


文字圖片

UIGraphicsBeginImageContext(self.tscroitleLabel.frame.size);

? ? [self.tscroitleLabel drawViewHierarchyInRect:self.tscroitleLabel.bounds afterScreenUpdates:NO];

? ? UIImage* image =? UIGraphicsGetImageFromCurrentImageContext();

? ? UIGraphicsEndImageContext();

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容