iOS通过按钮点击异步加载图片代码

iOS通过按钮点击异步加载图片代码,第1张

概述iOS通过按钮点击异步加载图片代码

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

@interface UIbutton (AsyncImage)//size by point- (voID)setimageFromURL:(Nsstring *)urlString adjustToSize:(CGSize)size completion:(voID (^)(voID))completion logo:(UIImage *)logoImage;@end@implementation UIbutton (AsyncImage)- (voID)setimageFromURL:(Nsstring *)urlString adjustToSize:(CGSize)size completion:(voID (^)(voID))completion logo:(UIImage *)logoImage{    dispatch_async(dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_DEFAulT,0),^{        UIImage *image = nil;        NSURL *url = [NSURL URLWithString:urlString];        NSData *data = [NSData dataWithContentsOfURL:url];        image = [UIImage imageWithData:data];        if (image) {            if (!CGSizeEqualToSize(size,CGSizeZero)) {                image = [UIImage imageWithCGImage:image.CGImage scale:[self scaleImage:image adjustToSize:size] orIEntation:image.imageOrIEntation];            }            if (logoImage) {                image = [self addlogoImage:logoImage toImage:image];            }            dispatch_async(dispatch_get_main_queue(),^{                [self setimage:image forState:UIControlStatenormal];                completion();            });        }        else {            NSLog(@"async load error.");        }    });}// 缩放图片以适应按钮大小- (CGfloat)scaleImage:(UIImage *)image adjustToSize:(CGSize)size{    CGfloat xScale = size.wIDth / image.size.wIDth;    CGfloat yScale = size.height / image.size.height;    return 1.0 / MIN(xScale,yScale);}- (UIImage *)addlogoImage:(UIImage *)logo toImage:(UIImage *)img{    //get image wIDth and height    CGfloat scale = [UIScreen mainScreen].scale;    int w = scale * img.size.wIDth;    int h = scale * img.size.height;    int logoWIDth = logo.scale * logo.size.wIDth;    int logoHeight = logo.scale * logo.size.height;    CGcolorSpaceRef colorSpace = CGcolorSpaceCreateDeviceRGB();    //create a graphic context with CGBitmapContextCreate    CGContextRef context = CGBitmapContextCreate(NulL,w,h,8,4 * w,colorSpace,kCGImageAlphaPremultiplIEdFirst);    CGContextDrawImage(context,CGRectMake(0,h),img.CGImage);    CGContextDrawImage(context,CGRectMake(w - logoWIDth,logoWIDth,logoHeight),[logo CGImage]);    CGImageRef imageMasked = CGBitmapContextCreateImage(context);    CGContextRelease(context);    CGcolorSpaceRelease(colorSpace);    return [UIImage imageWithCGImage:imageMasked scale:scale orIEntation:img.imageOrIEntation];}@end

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的iOS通过按钮点击异步加载图片代码全部内容,希望文章能够帮你解决iOS通过按钮点击异步加载图片代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1105960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存