iOS项目开发中遇到的问题

本文讲述了在iOS项目开发中遇到的三个问题:加载大图导致内存崩溃,截图时的内存泄漏,以及实现弹幕功能的挑战。针对大图加载,采取了裁剪图片的方法;截图问题通过改进实现方式得以解决;在实现弹幕功能时,选择了OCBarrage库,并自行实现了防碰撞功能。
摘要由CSDN通过智能技术生成
1、加载大图,内存崩溃

问题描述:在加载多张高清大图时,会出现崩溃的现象。
解决方案:客户端在显示缩略图时,将宽度大于320的图片,裁剪为宽度为320,高度等比例缩小的图片。

//代码调用方式
[imgView setImageWithURL:[NSURL URLWithString:imageUrl]
                         placeholder:DefaultIcon
                             options:kNilOptions
                             manager:[MGCircleHelper bigImageManager]
                            progress:nil
                           transform:nil
                          completion:nil];
//MGCircleHelper.m
static const CGFloat MGCircleHelperTransformWidthValue = 320;

@implementation MGCircleHelper

+ (YYWebImageManager *)bigImageManager {
   
    static YYWebImageManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
   
        NSString *path = [[UIApplication sharedApplication].cachesPath stringByAppendingPathComponent:@"migu.bigImage"];
        YYImageCache *cache = [[YYImageCache alloc] initWithPath:path];
        manager = [[YYWebImageManager alloc] initWithCache:cache queue:[YYWebImageManager sharedManager].queue];
        manager.sharedTransformBlock = ^(UIImage *image, NSURL *url) {
   
            if (!image || image.size.width <= MGCircleHelperTransformWidthValue) {
   
                return image;
            }
            CGFloat aspectRatio = image.size.width / image.size.height;
            CGFloat resultHeight = MGCircleHelperTransformWidthValue / aspectRatio;
            
            return [image imageByResizeToSize:CGSizeMake(MGCircleHelperTransformWidthValue, resultHeight)]; 
        };
    });
    return manager;
}

@end
2、截图,出现内存泄漏问题

问题描述:排查内存泄漏时,发现网上通用的截屏实现方式,会出现内存泄漏
解决方案:修改截屏的实现方式

 - (UIImage *)screenshot {
   
     UIWindow *window = [UIApplication sharedApplication].keyWindow;
     UIGraphicsBeginImageContextWithOptions(window.bounds.size, YES, [UIScreen mainScreen].scale);
    [window.layer renderInContext:UIGraphicsGetCurrentContext()]; 
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
    return image; 
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值