Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将CGContextRef保存为.PNG文件?_Ios_Cocoa Touch_Cgcontextref - Fatal编程技术网

Ios 如何将CGContextRef保存为.PNG文件?

Ios 如何将CGContextRef保存为.PNG文件?,ios,cocoa-touch,cgcontextref,Ios,Cocoa Touch,Cgcontextref,我创建了一个绘图iOS应用程序,我想在其中创建一个“保存图像”方法。 绘图在touchesMoved:方法中进行 - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; [self drawToCache:touch]; [self setNeedsDisplay]; } - (void) drawToCache:(

我创建了一个绘图iOS应用程序,我想在其中创建一个“保存图像”方法。 绘图在touchesMoved:方法中进行

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    [self drawToCache:touch];
    [self setNeedsDisplay];
}

- (void) drawToCache:(UITouch*)touch {
    hue += 0.005;
    if(hue > 1.0) hue = 0.0;
    UIColor *color = [UIColor colorWithHue:hue saturation:0.7 brightness:1.0 alpha:1.0];

    CGContextSetStrokeColorWithColor(cacheContext, [color CGColor]);
    CGContextSetLineCap(cacheContext, kCGLineCapRound);
    CGContextSetLineWidth(cacheContext, 15);

    CGPoint lastPoint = [touch previousLocationInView:self];
    CGPoint newPoint = [touch locationInView:self];

    CGContextMoveToPoint(cacheContext, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(cacheContext, newPoint.x, newPoint.y);
    CGContextStrokePath(cacheContext);

    CGRect dirtyPoint1 = CGRectMake(lastPoint.x-10, lastPoint.y-10, 20, 20);
    CGRect dirtyPoint2 = CGRectMake(newPoint.x-10, newPoint.y-10, 20, 20);
    [self setNeedsDisplayInRect:CGRectUnion(dirtyPoint1, dirtyPoint2)];
}
我想把我画的东西保存到一个.PNG文件中

有解决方案吗?

非常简单:

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:@"myImage.png"];
问题很简单:

UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:@"myImage.png"];

您需要实际设置路径位置。扩展CrimsonDiego的代码,这将把图像放在应用程序的/Documents目录中

NSString*docsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString*path=[docsdirectorystringbyappendingpathcomponent:@“myImage.png”];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
NSData*imageData=UIImagePNGRepresentation(图像);
[imageData WriteFile:原子路径:是];

您需要实际设置路径位置。扩展CrimsonDiego的代码,这将把图像放在应用程序的/Documents目录中

NSString*docsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString*path=[docsdirectorystringbyappendingpathcomponent:@“myImage.png”];
UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
NSData*imageData=UIImagePNGRepresentation(图像);
[imageData WriteFile:原子路径:是];

此解决方案对我不起作用。文件未出现在我的应用程序文件夹中(~/Library/application Support/iPhone Simulator/5.1/Applications/CCAA9512-D2D6-4312-83D6-A4A9F74906C3/*)。我应该在哪里写这段代码?这个解决方案不适合我。文件未出现在我的应用程序文件夹中(~/Library/application Support/iPhone Simulator/5.1/Applications/CCAA9512-D2D6-4312-83D6-A4A9F74906C3/*)。我应该在哪里写这段代码?我在第“[imageData writeToFile:path];”行得到一个错误。错误:“NSData”没有可见的@interface声明选择器“writeToFile:”使用正确的方法编辑了我的答案,writeToFile:原子性:如果此答案对您有帮助,请接受它。我在“[imageData writeToFile:path];”行遇到错误。错误:“NSData”没有可见的@interface声明选择器“writeToFile:”使用正确的方法编辑了我的答案,writeToFile:原子性:如果此答案对您有帮助,请接受它。可能重复的可能重复的