gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:19:06 24 4
gpt4 key购买 nike

我尝试实现类似 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)drawingViewDidPan:(UIPanGestureRecognizer*)sender
{
CGPoint currentDraggingPosition = [sender locationInView:_drawingView];

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

if(sender.state != UIGestureRecognizerStateEnded){
[self drawLine:_prevDraggingPosition to:currentDraggingPosition];
}
_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);

[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。有帮助吗?

最佳答案

来自 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com