IOS開發基礎知識--片段30,ios基礎知識--片段
1:ios 相簿操作 ALAssetsLibrary 知識點
a ALAssetsLibrary 執行個體為我們提供了擷取相簿(照片app)中的圖片和視頻的功能。在ios8 photos framework代替了ALAssetsLibrary。
在使用ALAssetsLibrary時,我們需要申明它的執行個體。
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
b. 迭代擷取相簿ALAssetsGroup:
- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock
ALASSetsGroupType類型:
ALAssetsGroupLibrary:從iTunes 來的相簿內容(如本身內建的向日葵照片)。
ALAssetsGroupAlbum:裝置自身產生或從iTunes同步來的照片,但是不包括照片流跟分享流中的照片。(例如從各個軟體中儲存下來的圖片)
ALAssetsGroupEvent 相機介面事件產生的相簿
ALAssetsGroupFaces 臉部相簿(具體不清楚)
ALAssetsGroupSavedPhotos 相機菲林照片
ALAssetsGroupPhotoStream 照片流
ALAssetsGroupAll 除了ALAssetsGroupLibrary上面所的內容。
例如:ALAssetsLibraryGroupsEnumerationResultsBlock
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) { ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos]; [group setAssetsFilter:onlyPhotosFilter]; if ([group numberOfAssets] > 0) { [self.imageGroup addObject:group]; } else { [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; } };
上面就是迭代AlAssetsGroup的block。每迭代一次就把相應的AlAssetsGroup儲存在一個可變的數組之中。AlAssetsGroup中的一些屬性工作表明了這個相簿的特徵。比如:
posterImage 相簿的縮圖
numberOfAssets 相簿中照片的數量
c:Asset 屬性
– valueForProperty:
1.ALAssetPropertyType 資源的類型(照片,視頻)
2.ALAssetPropertyLocation 資源地理位置(無位置資訊返回null)
3.ALAssetPropertyDuation 播放時間長度(照片返回ALErorInvalidProperty)
4.ALAssetPropertyOrientation 方向(共有8個方向,參見:ALAssetOrientation)
5.ALAssetPropertyDate 拍攝時間(包含了年與日時分秒)
6.ALAssetPropertyRepresentations 描述(列印看了下,只有帶尾碼的名稱)
7.ALAssetPropertyURLs(返回一個字典,索引值分別是檔案名稱和檔案的url)
8.ALAssetPropertyAssetURL 檔案的url )
editable property(指示資源是否可以編輯,唯讀屬性)
originalAsset property(原始資源。若沒有儲存修改後資源,則原始資源為nil)
for (ALAsset *asset in assets) { NSURL *assetURL= [asset valueForProperty:ALAssetPropertyAssetURL]; }
Accessing Representations
– defaultRepresentation
– representationForUTI:
– thumbnail(小正方形的縮圖)
– aspectRatioThumbnail(按原始資源長寬比例的縮圖)
另外一個小執行個體:
UIImage* ni = [UIImage imageNamed:@"new.png"]; //修改指定路徑的圖片資源內容,替換掉原來的內容 [asset setImageData:UIImageJPEGRepresentation(ni, 1.0) metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { NSLog(@"new:%@",assetURL); }]; //根據給定的圖片內容,重建一張新圖 [asset writeModifiedImageDataToSavedPhotosAlbum:UIImageJPEGRepresentation(ni, 1.0) metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { NSLog(@"new:%@",assetURL); }]; //擷取資源圖片的詳細資源資訊 ALAssetRepresentation* representation = [asset defaultRepresentation]; //擷取資源圖片的長寬 CGSize dimension = [representation dimensions]; //擷取資源圖片的高清圖 [representation fullResolutionImage]; //擷取資源圖片的全屏圖 [representation fullScreenImage]; //擷取資源圖片的名字 NSString* filename = [representation filename]; NSLog(@"filename:%@",filename); //縮放倍數 [representation scale]; //圖片資源容量大小 [representation size]; //圖片資源原資料 [representation metadata]; //旋轉方向 [representation orientation]; //資源圖片url地址,該地址和ALAsset通過ALAssetPropertyAssetURL擷取的url地址是一樣的 NSURL* url = [representation url]; NSLog(@"url:%@",url); //資源圖片uti,唯一標示符 NSLog(@"uti:%@",[representation UTI]);
2:Attribute運用(幾段代碼)
其中addAttribute可以增加不同的屬性,有些屬性值是要在NSMutableParagraphStyle裡面進行設定例如下面第一段代碼中NSParagraphStyleAttributeName,有些可以直接設定值如NSForegroundColorAttributeName
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setLineSpacing:5]; [muttext addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, muttext.length)]; [muttext addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, muttext.length)]; _myLabel.attributedText = muttext;
NSMutableParagraphStyle *paragraph=[[NSMutableParagraphStyle alloc]init]; paragraph.alignment=NSTextAlignmentCenter;//置中 NSDictionary* attrs =@{NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:30],//文本的顏色 字型 大小 NSForegroundColorAttributeName:[UIColor redColor],//文字顏色 NSParagraphStyleAttributeName:paragraph,//段落格式 // NSBackgroundColorAttributeName:[UIColor blueColor],//背景色 NSStrokeWidthAttributeName:@3, //描邊寬度 NSStrokeColorAttributeName:[UIColor greenColor],//設定 描邊顏色,和NSStrokeWidthAttributeName配合使用,設定了這個NSForegroundColorAttributeName就失效了 // NSStrikethroughStyleAttributeName:@1,//刪除線,數字代表線條寬度 NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),//底線,值為一個枚舉類型,大家可以分別試試 };
-(NSMutableAttributedString*)setTitle{ NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"準備量房"]; [title addAttribute:NSForegroundColorAttributeName value:COLOR_NAV_TITLE range:NSMakeRange(0, title.length)]; [title addAttribute:NSFontAttributeName value:SYSTEMFONT(18) range:NSMakeRange(0, title.length)]; return title;}
1.如果只是靜態顯示textView的內容為設定的行間距,執行如下代碼:// textview 改變字型的行間距 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 10;// 字型的行間距 NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:15], NSParagraphStyleAttributeName:paragraphStyle }; textView.attributedText = [[NSAttributedString alloc] initWithString:@"輸入你的內容" attributes:attributes]; 2.如果是想在輸入內容的時候就按照設定的行間距進行動態改變,那就需要將上面代碼放到textView的delegate方法裡-(void)textViewDidChange:(UITextView *)textView{ // textview 改變字型的行間距 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 20;// 字型的行間距 NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:15], NSParagraphStyleAttributeName:paragraphStyle }; textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes]; }
3:中文IME的鍵盤上有聯想、推薦的功能,所以可能導致常值內容長度上有些不符合預期,導致越界
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'
處理方式如下(textView.markedTextRange == nil)
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if (textView.text.length >= self.textLengthLimit && text.length > range.length) { return NO; } return YES;}- (void)textViewDidChange:(UITextView *)textView{ self.placeholder.hidden = (self.textView.text.length > 0); if (textView.markedTextRange == nil && self.textLengthLimit > 0 && self.text.length > self.textLengthLimit) { textView.text = [textView.text substringToIndex:self.textLengthLimit]; }}