NSData转Byte数组分段上传
作者:互联网
UIImage *image = [UIImage imageNamed:@"123"]; NSData *data = UIImagePNGRepresentation(image); // 每次读取1024的长度 NSInteger readLength = 1024; NSInteger count = data.length / readLength; NSMutableData *mutData = [NSMutableData data]; char bytes[data.length]; for (int i = 0; i < count; i++) { [data getBytes:bytes range:NSMakeRange(i * readLength, readLength)]; NSData *newData = [NSData dataWithBytes:bytes length:readLength]; [mutData appendData:newData]; } // 最后剩余的部分 NSInteger res = data.length % readLength; if (res > 0) { [data getBytes:bytes range:NSMakeRange(data.length - res, res)]; NSData *newData = [NSData dataWithBytes:bytes length:res]; [mutData appendData:newData]; }
NSLog(@"resulteData:%ld", mutData.length);
标签:res,上传,bytes,length,readLength,NSData,Byte,data 来源: https://www.cnblogs.com/dashengios/p/16511697.html