• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 当我在图片上实现类似 Snapchat 的涂鸦时出现内存问题(包括详细信息)

[复制链接]
菜鸟教程小白 发表于 2022-12-13 03:51:26 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我尝试实现类似 Snapchat 的功能,允许用户在拍摄的照片上绘制东西。

基本上,我有两种观点。 (这里有一些代码)

UIImageView *imageView; //holding the taken picture (original image)
UIImageView *drawingView; //the view on which all drawing happens

/* this is the action triggered by gesture recognizer */
- (void)drawingViewDidPanUIPanGestureRecognizer*)sender
{
    CGPoint currentDraggingPosition = [sender locationInView:_drawingView];

    if(sender.state == UIGestureRecognizerStateBegan){
        _prevDraggingPosition = currentDraggingPosition;
    }

    if(sender.state != UIGestureRecognizerStateEnded){
        [self drawLine:_prevDraggingPosition to:currentDraggingPosition];
    }
    _prevDraggingPosition = currentDraggingPosition;
}

-(void)drawLineCGPoint)from toCGPoint)to
{
    CGSize size = _drawingView.size;
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGFloat strokeWidth = MAX(1, _widthSlider.value * 65);
    UIColor *strokeColor = _strokePreview.backgroundColor;

    CGContextSetLineWidth(context, strokeWidth);
    CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextMoveToPoint(context, from.x, from.y);
    CGContextAddLineToPoint(context, to.x, to.y);
    CGContextStrokePath(context);

    _drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

/* this is the method to build final image, I overlay the image from drawing on top of the imageView */
- (UIImage*)buildImage
{
    CGSize _originalImageSize = imageView.image.size;
    UIGraphicsBeginImageContextWithOptions(_originalImageSize, NO, 0.0);

    [imageView.image drawAtPoint:CGPointZero];
    [drawingView.image drawInRect:CGRectMake(0, 0, _originalImageSize.width, _originalImageSize.height)];

    UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return tmp;
}

调用 buildImage() 时,我的应用程序因内存警告而崩溃。我正在 iPhone 4s(512 内存)上进行测试,全屏照片的分辨率为 2448x3264。有什么帮助吗?



Best Answer-推荐答案


来自 UIImage 类引用:

You should avoid creating UIImage objects that are greater than 1024 x 1024 in size. Besides the large amount of memory such an image would consume, you may run into problems when using the image as a texture in OpenGL ES or when drawing the image to a view or layer. This size restriction does not apply if you are performing code-based manipulations, such as resizing an image larger than 1024 x 1024 pixels by drawing it to a bitmap-backed graphics context. In fact, you may need to resize an image in this manner (or break it into several smaller images) in order to draw it to one of your views.

我建议使用小得多的图像。

关于ios - 当我在图片上实现类似 Snapchat 的涂鸦时出现内存问题(包括详细信息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27392862/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap