iOS - 實(shí)用Tips

一、后臺有媒體播放器時(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;
    
    
    
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市七芭,隨后出現(xiàn)的幾起案子素挽,更是在濱河造成了極大的恐慌,老刑警劉巖狸驳,帶你破解...
    沈念sama閱讀 221,888評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件预明,死亡現(xiàn)場離奇詭異,居然都是意外死亡耙箍,警方通過查閱死者的電腦和手機(jī)撰糠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,677評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來辩昆,“玉大人阅酪,你說我怎么就攤上這事≈耄” “怎么了术辐?”我有些...
    開封第一講書人閱讀 168,386評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長扇丛。 經(jīng)常有香客問我术吗,道長,這世上最難降的妖魔是什么帆精? 我笑而不...
    開封第一講書人閱讀 59,726評論 1 297
  • 正文 為了忘掉前任较屿,我火速辦了婚禮,結(jié)果婚禮上卓练,老公的妹妹穿的比我還像新娘隘蝎。我一直安慰自己,他們只是感情好襟企,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,729評論 6 397
  • 文/花漫 我一把揭開白布嘱么。 她就那樣靜靜地躺著,像睡著了一般顽悼。 火紅的嫁衣襯著肌膚如雪曼振。 梳的紋絲不亂的頭發(fā)上几迄,一...
    開封第一講書人閱讀 52,337評論 1 310
  • 那天,我揣著相機(jī)與錄音冰评,去河邊找鬼映胁。 笑死,一個(gè)胖子當(dāng)著我的面吹牛甲雅,可吹牛的內(nèi)容都是我干的解孙。 我是一名探鬼主播,決...
    沈念sama閱讀 40,902評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼抛人,長吁一口氣:“原來是場噩夢啊……” “哼弛姜!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起妖枚,我...
    開封第一講書人閱讀 39,807評論 0 276
  • 序言:老撾萬榮一對情侶失蹤廷臼,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后盅惜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體中剩,經(jīng)...
    沈念sama閱讀 46,349評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,439評論 3 340
  • 正文 我和宋清朗相戀三年抒寂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了结啼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,567評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡屈芜,死狀恐怖郊愧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情井佑,我是刑警寧澤属铁,帶...
    沈念sama閱讀 36,242評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站躬翁,受9級特大地震影響焦蘑,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜盒发,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,933評論 3 334
  • 文/蒙蒙 一例嘱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宁舰,春花似錦拼卵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,420評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春即寡,著一層夾襖步出監(jiān)牢的瞬間徊哑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,531評論 1 272
  • 我被黑心中介騙來泰國打工聪富, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留实柠,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,995評論 3 377
  • 正文 我出身青樓善涨,卻偏偏與公主長得像,于是被迫代替她去往敵國和親草则。 傳聞我的和親對象是個(gè)殘疾皇子钢拧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,585評論 2 359

推薦閱讀更多精彩內(nèi)容