【问题标题】:Drawing images saved in our app绘制保存在我们的应用程序中的图像
【发布时间】:2016-07-19 12:34:09
【问题描述】:

我正在画图。并将该图像保存在相机胶卷中,当我保存该图像时,图像 alpha 设置不正确。如果我用 80% 的 alpha 绘制图像,然后单击保存图像将保存 40%(大约)的 alpha。我没有为此使用任何库。我正在使用触摸绘图。

代码是

(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{


    UITouch *touch = [[event allTouches] anyObject];
    currentPoint = [touch locationInView:self.tempimage];
    UIGraphicsBeginImageContext(self.tempimage.frame.size);

    [tempimage.image drawInRect:CGRectMake(0, 0, self.tempimage.frame.size.width, self.tempimage.frame.size.height)];

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

    strpoint=NSStringFromCGPoint(currentPoint);
    strpoint1=NSStringFromCGPoint(lastPoint);

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextRef context = UIGraphicsGetCurrentContext();


    if ([linewidth isEqualToString:@"40"])
    {
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 40.0);
        lineWidth=40.0;
    }
    if ([linewidth isEqualToString:@"30"])
    {
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0);
        lineWidth=15.0;
    }
    if ([linewidth isEqualToString:@"20"])
    {
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
        lineWidth=5.0;
    }

    if  ([streraser isEqualToString:@"eraser"])
    {
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
    }
    /*CGContextSetAlpha(context,opacity);
        CGContextSetStrokeColorWithColor(context,newcolor.CGColor);

//    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), self->red, self->green, self->blue, self->opacity);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),blendmode);*/
//    [self.view.layer renderInContext:context];


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    lastPoint=[touch locationInView:tempimage];

NSLog(@"Touch starting point = x : %f Touch Starting Point = y : %f", 

lastPoint.x, lastPoint.y);
    myPath=[[UIBezierPath alloc]init];
    myPath.lineWidth=self.lineWidth;
    myPath.lineCapStyle = kCGLineCapRound;
 redoIndex =0;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 UIGraphicsBeginImageContext(self.tempimage.bounds.size);
[self.tempimage.layer renderInContext:UIGraphicsGetCurrentContext()];
rawImage = UIGraphicsGetImageFromCurrentImageContext();
[tempimage setAlpha:self.lineAlpha];
 UIGraphicsEndImageContext();
#if PUSHTOFILE
   lineIndex++;
    [self performSelectorInBackground:@selector(writeFilesBG)
                           withObject:nil];
#else
    NSDictionary *lineInfo = [NSDictionary dictionaryWithObjectsAndKeys:rawImage, @"IMAGE",nil];
    [pointsArray addObject:lineInfo];
    UIBezierPath *_path=[pointsArray lastObject];
    [_stack addObject:_path];
    [pointsArray removeLastObject];
    [self.tempimage setNeedsDisplay];
#endif

}

- (IBAction)btnsave:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:Save_Folder_Name,@"Cancel", nil];
    [actionSheet showInView:self.view];

}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        UIGraphicsBeginImageContextWithOptions(self.tempimage.bounds.size, NO, 0.0);
//        [self.tempimage.image drawInRect:CGRectMake(0, 0, self.tempimage.frame.size.width, self.tempimage.frame.size.height)];
        [self.tempimage.image drawInRect:CGRectMake(0, 0, tempimage.frame.size.width, tempimage.frame.size.height) blendMode:blendmode alpha:lineAlpha];
        UIImage *SaveImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(SaveImage, self,@selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    // Was there an error?
    if (error != NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Image could not be saved.Please try again"  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Close", nil];
        [alert show];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image was successfully saved in photoalbum"  delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Close", nil];
        [alert show];
    }
}

任何帮助将不胜感激。

【问题讨论】:

标签: iphone cocoa-touch camera


【解决方案1】:

我使用了两个图像视图,一个使用 tempimage,一个使用 imageview,我只在 tempimage 中设置了 alpha,但是在两个图像视图中都设置了 alpha,然后我的问题就解决了 分别称为 imageview(alpha 为 1.0)和 tempimage(alpha 为 0.5),我可以像这样保存快照:

- (void)saveSnapshot {
CGRect rect = self.tempimage.bounds;

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    if ([self.tempimage respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        [self.imageview drawViewHierarchyInRect:rect afterScreenUpdates:YES]; // iOS7+
        [self.tempimage drawViewHierarchyInRect:rect afterScreenUpdates:YES];
    } else {
        [self.imageview.layer renderInContext:UIGraphicsGetCurrentContext()]; // pre iOS7
        [self.tempimage.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多