App的打赏功能

前言

  • 上一篇技术文章介绍了支付宝、微信、银联三大支付的集成步骤和文档说明。
  • 今天带大家一起来分析实现下App的打赏功能,步入正题前先说下现在打赏功能的重要性,微信红包功能在掀起一阵狂潮之后,热度已经散的差不多了,而逐渐崭露头角的打赏功能,已经流行在大部分的App里面。

  • 譬如支付宝、简书、新浪微博、Bilibili动画、落网等等。

  • 那些网络主播,明明可以被用户免费看,却经常收获总额几十万的打赏。

  • 由此可见打赏功能的重要性,至于如何让你的纯干货刺激到用户打赏,关注我的简书,有文章说明。

好了,闲话不在多说,现在步入正题,以我最近项目为例来简述打赏功能的实现,支付方式微信。

打赏界面实现

  1. 利用AutoLayout很容易实现,UI这块的布局实现忽略,看效果图。
    打赏界面
  2. 引入微信开发者平台的sdk,具体见上篇文章,现在只介绍关键代码。
  3. 需要用到的两个类文件
1
2
3
WXApiManager.h

WXApiRequestHandler.h
  1. 点击按钮跳转微信
1
2
3
4
5
6
7
- (IBAction)onePayAction:(id)sender {
NSString *price = @"100";
NSString *rewardTitle = [NSString
stringWithFormat:@"打赏独立音乐人:%@",_playerModel.songer];
NSString *response = [WXApiRequestHandler jumpToBizPay: price shopName:_rewardString];
[self payFailAlert: response];
}
  1. WXApiRequestHandler.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
+ (NSString *)jumpToBizPay:(NSString *)price shopName:(NSString *)shopName{

//============================================================
// V3&V4支付流程实现
// 注意:参数配置请查看服务器端Demo
// 更新时间:2015年11月20日
//============================================================
//https://api.mch.weixin.qq.com/pay/unifiedorder
//http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios
BOOL isConnect = [CommonUtil connectedToNetwork];
if (!isConnect) {
return @"网络不给力呀~";
}else{
NSString *url = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
NSLog(@"payUrl:%@",url);
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[[HttpAFNetworing shareInstance] httpMgr] GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"responseObject:%@",responseObject);
NSDictionary *dict = responseObject;
if(responseObject != nil){
NSMutableString *retcode = [dict objectForKey:@"retcode"];
if (retcode.intValue == 0){
NSMutableString *stamp = [dict objectForKey:@"timestamp"];

//调起微信支付
PayReq* req = [[PayReq alloc] init];
req.partnerId = [dict objectForKey:@"partnerid"];
req.prepayId = [dict objectForKey:@"prepayid"];
req.nonceStr = [dict objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [dict objectForKey:@"package"];
req.sign = [dict objectForKey:@"sign"];
[WXApi sendReq:req];

//日志输出
NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[CommonUtil NoNetWorkNotiTip];
}];
}

return @"";
}
1
2
3
4
5
6
7
8
- (void)payFailAlert:(NSString *)resp
{
if( ![@"" isEqual:resp] ){
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"支付失败" message:resp delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alter show];
}
}

打赏完成界面实现

UI布局AutoLayout实现,不再贴代码,模糊代码实现如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//加模糊效果,image是图片,blur是模糊度
- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
//模糊度,
if ((blur < 0.1f) || (blur > 2.0f)) {
blur = 0.5f;
}

//boxSize必须大于0
int boxSize = (int)(blur * 100);
boxSize -= (boxSize % 2) + 1;
NSLog(@"boxSize:%i",boxSize);
//图像处理
CGImageRef img = image.CGImage;
//需要引入
/*
This document describes the Accelerate Framework, which contains C APIs for vector and matrix math, digital signal processing, large number handling, and image processing.
本文档介绍了Accelerate Framework,其中包含C语言应用程序接口(API)的向量和矩阵数学,数字信号处理,大量处理和图像处理。
*/

//图像缓存,输入缓存,输出缓存
vImage_Buffer inBuffer, outBuffer;
vImage_Error error;
//像素缓存
void *pixelBuffer;

//数据源提供者,Defines an opaque type that supplies Quartz with data.
CGDataProviderRef inProvider = CGImageGetDataProvider(img);
// provider’s data.
CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);

//宽,高,字节/行,data
inBuffer.width = CGImageGetWidth(img);
inBuffer.height = CGImageGetHeight(img);
inBuffer.rowBytes = CGImageGetBytesPerRow(img);
inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);

//像数缓存,字节行*图片高
pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));

outBuffer.data = pixelBuffer;
outBuffer.width = CGImageGetWidth(img);
outBuffer.height = CGImageGetHeight(img);
outBuffer.rowBytes = CGImageGetBytesPerRow(img);


// 第三个中间的缓存区,抗锯齿的效果
void *pixelBuffer2 = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
vImage_Buffer outBuffer2;
outBuffer2.data = pixelBuffer2;
outBuffer2.width = CGImageGetWidth(img);
outBuffer2.height = CGImageGetHeight(img);
outBuffer2.rowBytes = CGImageGetBytesPerRow(img);

//Convolves a region of interest within an ARGB8888 source image by an implicit M x N kernel that has the effect of a box filter.
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer2, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
error = vImageBoxConvolve_ARGB8888(&outBuffer2, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);


if (error) {
NSLog(@"error from convolution %ld", error);
}

// NSLog(@"字节组成部分:%zu",CGImageGetBitsPerComponent(img));
//颜色空间DeviceRGB
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//用图片创建上下文,CGImageGetBitsPerComponent(img),7,8
CGContextRef ctx = CGBitmapContextCreate(
outBuffer.data,
outBuffer.width,
outBuffer.height,
8,
outBuffer.rowBytes,
colorSpace,
CGImageGetBitmapInfo(image.CGImage));

//根据上下文,处理过的图片,重新组件
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage *returnImage = [UIImage imageWithCGImage:imageRef];

//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);

free(pixelBuffer);
free(pixelBuffer2);
CFRelease(inBitmapData);

CGColorSpaceRelease(colorSpace);
CGImageRelease(imageRef);

return returnImage;
}

打赏完成界面

留言成功后分享打赏界面

UI布局AutoLayout实现,不再贴代码

留言完成分享打赏界面

结束

  • 如有问题请留言。

坚持原创技术分享,您的支持将鼓励我继续创作!
欢迎大家关注:奇点日报微信公众号,每天推送精选技术文章