一、后臺有媒體播放器時(shí):
當(dāng)其他應(yīng)用程序占用媒體播放器時(shí)翔悠,不能影響到后臺的媒體音樂播放业崖;
當(dāng)你的應(yīng)用程序中有相機(jī)要拍照時(shí),不會(huì)影響到后臺音樂程序的播放
// 當(dāng)其他應(yīng)用程序占用媒體播放器時(shí)蓄愁,不能影響到后臺的媒體音樂播放
NSError *error;
BOOL success = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if (!success) { //Handle error
AXLog(@"%@", [error localizedDescription]);
} else {
// Yay! It worked! 當(dāng)你的應(yīng)用程序中有相機(jī)要拍照時(shí)双炕,不會(huì)影響到后臺音樂程序的播放
AXLog(@"123456789");
}
二、屏幕常亮
/// 屏幕長亮
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
三撮抓、NSString 轉(zhuǎn)換為 int64妇斤、int32 (微信支付時(shí)遇到)
// NSString 轉(zhuǎn)換為 int64
UInt64 timeStamp = atoll([payItem.timestamp UTF8String]);
// NSString 轉(zhuǎn)換為 int32
UInt32 timeStamp = atoi([payItem.timestamp UTF8String]);
四、float變量 值為NaN
“這種錯(cuò)誤是在float經(jīng)過函數(shù)運(yùn)行出了不是數(shù)字的值,nan的意思就是not a number趟济。
主要常見原因:
1.除以0
2.sizeWithFont的字符串為nil
3.數(shù)學(xué)函數(shù)不正確運(yùn)算
解決方法除了排除根源所在之外,用函數(shù)isnan()也是不錯(cuò)的選擇(至少在沒有徹底解決以前)
如下:
float x = NAN;
if (!isnan(x)) {
view.frame = CGRectMake(x, 8, 10, 12);
}”
五咽笼、CFNetwork internal error
CFNetwork internal error (0xc01a:/BuildRoot/Library/Caches/com.apple.xbs/Sources/CFNetwork_Sim/CFNetwork-808.2.16/Loading/URLConnectionLoader.cpp:304)
訪問的url地址不對 不存在等
六顷编、獲取網(wǎng)絡(luò)圖片大小
①阿里云OSS圖片處理,鏈接:
https://help.aliyun.com/document_detail/44976.html?spm=5176.doc44975.6.952.aGGXXG
下面是iOS端獲取剑刑,若是網(wǎng)絡(luò)圖片顯示在UIImageView上媳纬,并且根據(jù)圖片大小決定UIImageView長寬的話,直接讓后臺去做這件事
#define kHeadUrl @"http://image.xxxxx.com/" // 項(xiàng)目阿里云存儲服務(wù)器地址
#define kSize_Image_Get @"x-oss-process=image/info"
+ (void)getImageSizePath:(NSString *)imagePath heightBlock:(ImgHeightBlock)heightBlock{
NSString *path = [NSString stringWithFormat:@"%@%@?%@",kHeadUrl,imagePath,kSize_Image_Get];
[NetWorkManager getOrPostWithType:Get withUrlString:path withParams:nil loadingImageArr:nil progress:^(float progress) {
} toShowView:nil success:^(NSDictionary *object) {
NSDictionary *wD = [object objectForKey:@"ImageWidth"];
NSDictionary *hD = [object objectForKey:@"ImageHeight"];
NSString *w = [wD objectForKey:@"value"];
NSString *h = [hD objectForKey:@"value"];
// CGFloat heightR = kWidth * [h floatValue] / [w floatValue];
if (heightBlock) {
// ... 根據(jù)項(xiàng)目所需自定義
}
} fail:^(NSError *error) {
AXLog(@"error : %@", error);
} showHUD:NO];
}
②NSData
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://xxx.jpg"]];
UIImage *image = [UIImage imageWithData:data];
AXLog(@"w = %f, h = %f",image.size.width,image.size.height);
③SDWebImage
[[[SDWebImageManager sharedManager] imageDownloader] downloadImageWithURL:[NSURL URLWithString:[AXUtils getImagePath:ad.img]] options:SDWebImageDownloaderIgnoreCachedResponse progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
if (image) {
AXLog(@"w = %f, h = %f",image.size.width,image.size.height);
}
}];
七施掏、獲取網(wǎng)絡(luò)圖片平均色
①阿里云OSS
#define kColor_Image_Get @"x-oss-process=image/average-hue"
+ (void)getImageColorPath:(NSString *)imagePath colorBlock:(ImgColorBlock)colorBlock{
NSString *path = [NSString stringWithFormat:@"%@%@?%@",kHeadUrl,imagePath,kColor_Image_Get];
[NetWorkManager getOrPostWithType:Get withUrlString:path withParams:nil loadingImageArr:nil progress:^(float progress) {
} toShowView:nil success:^(NSDictionary *object) {
NSString *color = [object objectForKey:@"RGB"];
// AXLog(@"%@",[color substringFromIndex:2]);
if (colorBlock) {
colorBlock([color substringFromIndex:2]);
}
} fail:^(NSError *error) {
AXLog(@"error : %@", error);
} showHUD:NO];
}
②參考:http://www.cocoachina.com/bbs/read.php?tid=181490
-(UIColor*)mostColor{
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
int bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
#else
int bitmapInfo = kCGImageAlphaPremultipliedLast;
#endif
//第一步 先把圖片縮小 加快計(jì)算速度. 但越小結(jié)果誤差可能越大
CGSize thumbSize=CGSizeMake(50, 50);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL,
thumbSize.width,
thumbSize.height,
8,//bits per component
thumbSize.width*4,
colorSpace,
bitmapInfo);
CGRect drawRect = CGRectMake(0, 0, thumbSize.width, thumbSize.height);
CGContextDrawImage(context, drawRect, self.CGImage);
CGColorSpaceRelease(colorSpace);
//第二步 取每個(gè)點(diǎn)的像素值
unsigned char* data = CGBitmapContextGetData (context);
if (data == NULL) return nil;
NSCountedSet *cls=[NSCountedSet setWithCapacity:thumbSize.width*thumbSize.height];
for (int x=0; x<thumbSize.width; x++) {
for (int y=0; y<thumbSize.height; y++) {
int offset = 4*(x*y);
int red = data[offset];
int green = data[offset+1];
int blue = data[offset+2];
int alpha = data[offset+3];
NSArray *clr=@[@(red),@(green),@(blue),@(alpha)];
[cls addObject:clr];
}
}
CGContextRelease(context);
//第三步 找到出現(xiàn)次數(shù)最多的那個(gè)顏色
NSEnumerator *enumerator = [cls objectEnumerator];
NSArray *curColor = nil;
NSArray *MaxColor=nil;
NSUInteger MaxCount=0;
while ( (curColor = [enumerator nextObject]) != nil )
{
NSUInteger tmpCount = [cls countForObject:curColor];
if ( tmpCount < MaxCount ) continue;
MaxCount=tmpCount;
MaxColor=curColor;
}
return [UIColor colorWithRed:([MaxColor[0] intValue]/255.0f) green:([MaxColor[1] intValue]/255.0f) blue:([MaxColor[2] intValue]/255.0f) alpha:([MaxColor[3] intValue]/255.0f)];
}
八钮惠、獲取本地視頻截圖
+ (UIImage *)getVideoImage:(NSString *)videoPath {
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoPath] options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 600);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return thumb;
}