菜鸟教程小白 发表于 2022-12-13 03:51:26

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


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

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

<pre><code>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)drawingViewDidPan:(UIPanGestureRecognizer*)sender
{
    CGPoint currentDraggingPosition = ;

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

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

-(void)drawLine:(CGPoint)from to:(CGPoint)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);

    ;
    ;

    UIImage *tmp = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return tmp;
}
</code></pre>

<p>调用 buildImage() 时,我的应用程序因内存警告而崩溃。我正在 <strong>iPhone 4s</strong>(512 内存)上进行测试,全屏照片的分辨率为 2448x3264。有什么帮助吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>来自 UIImage 类引用:</p>

<blockquote>
<p>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.</p>
</blockquote>

<p>我建议使用小得多的图像。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 当我在图片上实现类似 Snapchat 的涂鸦时出现内存问题(包括详细信息),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27392862/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27392862/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 当我在图片上实现类似 Snapchat 的涂鸦时出现内存问题(包括详细信息)