UIActionSheet--从相册去出照片进行涂抹,截图保存到相册的demo完成

在继承UIView 创建PIDrawerView类,.m中申明一个枚举类型NS_ENUM,set 与get 画线类型DrawingMode和颜色设置UIColor

#import <UIKit/UIKit.h>

typedef NS_ENUM(NSInteger, DrawingMode) {

DrawingModeNone = 0,

DrawingModePaint,

DrawingModeErase,

};

@interface PIDrawerView : UIView

@property (nonatomic, readwrite) DrawingMode drawingMode;

@property (nonatomic, strong) UIColor *selectedColor;

@end

在在继承UIView.h文件中实现申明

#import "PIDrawerView.h"

@interface PIDrawerView ()

{

CGPoint previousPoint;

CGPoint currentPoint;

}

@property (nonatomic, strong) UIImage * viewImage;

@end

@implementation PIDrawerView

- (void)awakeFromNib

{

[self initialize];

}

- (void)drawRect:(CGRect)rect

{

[self.viewImage drawInRect:self.bounds];

}

#pragma mark - setter methods

- (void)setDrawingMode:(DrawingMode)drawingMode

{

_drawingMode = drawingMode;

}

#pragma mark - Private methods

- (void)initialize

{

currentPoint = CGPointMake(0, 0);

previousPoint = currentPoint;

_drawingMode = DrawingModeNone;

_selectedColor = [UIColor blackColor];

}

- (void)eraseLine

{

UIGraphicsBeginImageContext(self.bounds.size);

[self.viewImage drawInRect:self.bounds];

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);

CGContextBeginPath(UIGraphicsGetCurrentContext());

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

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

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

CGContextStrokePath(UIGraphicsGetCurrentContext());

self.viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

previousPoint = currentPoint;

[self setNeedsDisplay];

}

- (void)drawLineNew

{

UIGraphicsBeginImageContext(self.bounds.size);

[self.viewImage drawInRect:self.bounds];

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.selectedColor.CGColor);

CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10);

CGContextBeginPath(UIGraphicsGetCurrentContext());

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

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

CGContextStrokePath(UIGraphicsGetCurrentContext());

self.viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

previousPoint = currentPoint;

[self setNeedsDisplay];

}

- (void)handleTouches

{

if (self.drawingMode == DrawingModeNone) {

// do nothing

}

else if (self.drawingMode == DrawingModePaint) {

[self drawLineNew];

}

else

{

[self eraseLine];

}

}

#pragma mark - Touches methods

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

CGPoint p = [[touches anyObject] locationInView:self];

previousPoint = p;

}

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

{

currentPoint = [[touches anyObject] locationInView:self];

[self handleTouches];

}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

currentPoint = [[touches anyObject] locationInView:self];

[self handleTouches];

}

在ViewController中引进 PIDrawerView类和MobileCoreServices/MobileCoreServices库文件

#import "ViewController.h"

#import <MobileCoreServices/MobileCoreServices.h>

#import "PIDrawerView.h"

@interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

{

PIDrawerView * viewdr;

UIView * viewimageView;

}

@property (nonatomic, strong) UIColor *selectedColor;

@property(nonatomic)UIImageView * backGroundImageView;

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return self;

}

- (void)viewDidLoad {

[super viewDidLoad];

viewimageView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 20, 350)];

[self.view addSubview:viewimageView];

_backGroundImageView = [[UIImageView alloc]initWithFrame:viewimageView.bounds];

_backGroundImageView.userInteractionEnabled = YES;

viewdr = [[PIDrawerView alloc]initWithFrame:viewimageView.bounds];

viewdr.userInteractionEnabled = YES;

viewdr.backgroundColor = [UIColor clearColor];

[viewimageView addSubview:_backGroundImageView];

[viewimageView addSubview:viewdr];

self.selectedColor = [UIColor blackColor];

//    开始画线

[viewdr setSelectedColor:self.selectedColor];

[viewdr setSelectedColor:self.selectedColor];

UIView * viewHight = [[UIView alloc]initWithFrame:CGRectMake(1, self.view.frame.size.height - 50, self.view.frame.size.width, 100)];

[self.view addSubview:viewHight];

UIButton * buttonq = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 150, 50)];

[buttonq setTitle:@"编辑涂抹" forState:UIControlStateNormal];

[buttonq addTarget:self action:@selector(onLick2) forControlEvents:UIControlEventTouchUpInside];

[buttonq setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[viewHight addSubview:buttonq];

UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(150, 0, 150, 50)];

[button setTitle:@"进入相册" forState:UIControlStateNormal];

[button addTarget:self action:@selector(onLick) forControlEvents:UIControlEventTouchUpInside];

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[viewHight addSubview:button];

UIButton * buttonsave = [[UIButton alloc]initWithFrame:CGRectMake(350, 0, 50, 50)];

[buttonsave setTitle:@"截屏" forState:UIControlStateNormal];

[buttonsave setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[buttonsave addTarget:self action:@selector(saveOnLick) forControlEvents:UIControlEventTouchUpInside];

[viewHight addSubview:buttonsave];

}

//保存

-(void)saveOnLick{

//            开始截屏

UIGraphicsBeginImageContextWithOptions(viewimageView.bounds.size, NO, 2.0);

//            路径

CGContextRef context = UIGraphicsGetCurrentContext();

//

[self.view.layer renderInContext:context];

//            图片

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

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

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

if (error == nil) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil ];

[alert show];

}else{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil ];

[alert show];

}

}

//进行划线

-(void)onLick2{

[viewdr setDrawingMode:DrawingModePaint];

}

//进入相册

-(void)onLick{

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Pick" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Pick From Library",nil];

[actionSheet showInView:self.view];

}

#pragma mark - UIActionSheetDelegate methods

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];

mediaUI.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

mediaUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeSavedPhotosAlbum];

mediaUI.allowsEditing = NO;

mediaUI.delegate = self;

[self presentViewController:mediaUI animated:YES completion:nil];

}

#pragma mark - UIImagePickerControllerDelegate methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

UIImage *image = nil;

if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)

{

image = [info objectForKey:UIImagePickerControllerOriginalImage];

}

else

{

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)

{

image = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

}

}

self.backGroundImageView.image = image;

[picker dismissViewControllerAnimated:YES completion:nil];

}

时间: 2024-12-16 10:38:41

UIActionSheet--从相册去出照片进行涂抹,截图保存到相册的demo完成的相关文章

Swift-贝赛尔曲线实现画图板 &amp;&amp; 截图保存到相册中

本文内容参考自 传送门.原文是用 OC 写的,我把它改成了 Swift 的. 我们先来看看效果图: 第一幅图是我们画了一个 "iOS" 的图像,第二幅图是我们点击保存成功,第三幅图是可以在相册中看到我们刚才画的图. 感觉很不错有木有?接下来我们就来说说是怎么实现的. 我们分两部分来说:上半部分的画图板和下半部分的控制区. 上半部分的画图板是我们自定义的 view,我们设置如下属性: class MyView: UIView { var color = UIColor.redColor(

裁剪图片,截图,保存到相册,水印

// --- 裁剪圆形图片 -画带边框的圆形头像 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // -1.获取图片 UIImage* image = [UIImage imageNamed:@"me"]; // -2.margin CGFloat margin = 10; // -3.上下文大小 CGSize size = CGSizeMake(image.size.width + 2 * margi

截图保存到相册

// 1.截图 UIImage *image = [UIImage captureWithView:self.paintView]; // 2.保存到图片 UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); /** 保存图片操作之后就会调用 */ - (void)image:(UIImage *)image didFinishSaving

jsp实现仿QQ空间新建多个相册名称,向相册中添加照片

工具:Eclipse,Oracle,smartupload.jar:语言:jsp,Java:数据存储:Oracle. 实现功能介绍: 主要是新建相册,可以建多个相册,在相册中添加多张照片,删除照片,删除相册,当相册下有照片时先删除照片才能删除相册. 因为每个相册和照片要有所属人,所以顺带有登录功能. 声明:只是后端实现代码,前台无任何样式,代码测试可行,仅供参考. 代码: 数据库连接帮助类: public class JDBCHelper { public static final String

获取某一个相册中所有照片和照片的缩略图

- (void)loadAssets { // Initialise _assets = [NSMutableArray new]; _assetLibrary = [[ALAssetsLibrary alloc] init]; // Run in the background as it takes a while to get all assets from the library dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE

iOSQuartz2D-04-手动剪裁图片并保存到相册

实现效果 操作步骤 绘制一个矩形框,弹出一个alertView,提示是否保存图片 点击"是",将图片保存到相册 在相册中查看保存的图片 效果图 实现思路 在控制器的view上添加一个imageView,设置图片 在控制器的view上添加一个pan手势 跟踪pan手势,绘制一个矩形框(图片的剪切区域) 在pan手势结束时,通过alertView提示"是否将图片保存至相册?" 点击"是",保存图片 点击"否",暂时什么都不做 实现

iOSQuartz2D-04-图片剪裁并保存至相册

实现效果 操作步骤 绘制一个矩形框,弹出一个alertView,提示是否保存图片 点击"是",将图片保存到相册 在相册中查看保存的图片 效果图 实现思路 在控制器的view上添加一个imageView,设置图片 在控制器的view上添加一个pan手势 跟踪pan手势,绘制一个矩形框(图片的剪切区域) 在pan手势结束时,通过alertView提示"是否将图片保存至相册?" 点击"是",保存图片 点击"否",暂时什么都不做 实现

iOS截屏保存至相册

#pragma mark 截屏并保存至相册 -(void)screenShotsComplete:(void(^)(UIImage * img)) complete { CGSize imageSize = [[UIScreen mainScreen] bounds].size; UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(

截图生成图片并保存到相册

// 保存到相册 $("#content").on("click", "#savepic", function () { $("#waitingupload").removeClass("heisebghid").addClass("heisebg"); // 调用html2canvas生成截图 html2canvas($("#orderInfo")[0], { al