用UIImagePickerController 类来获取视频,大体分为以下几个步骤:
1 初始化UIImagePickerController 类;
2 设置UIImagePickerController 实例的数据来源类型(下面解释);
3 设置设置代理;
4 如果需要做修改的话设置allowsEditing =yes。
数据来源类型一共有三种:
enum {
UIImagePickerControllerSourceTypePhotoLibrary ,//来自图库
UIImagePickerControllerSourceTypeCamera ,//来自相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum //来自相册
};
在用这些来源的时候最好检测以下设备是否支持;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
NSLog(@"支持相机");
}
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
NSLog(@"支持图库");
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
NSLog(@"支持相片库");
}
调用摄像头来获取资源
- (void)viewDidLoad {
[super viewDidLoad];
picker = [[UIImagePickerController alloc]init];
pickerviewbackgroundColor = [UIColor orangeColor];
UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
pickersourceType = sourcheType;
pickerdelegate = self;
pickerallowsEditing = YES;
}
上面只是实例了UIImagePickerController及其属性 在需要获取的时候需要d出窗口调用
[self presentViewController:picker animated:YES completion:nil];
我们还需要代理来获取我们选中的
UIImagePickerControllerDelegate
代理中一共三个方法 其中一个30 已经废弃了,只剩下两个我们需要用的
- (void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary
)info;
当用户选取完成后调用;
- (void)imagePickerControllerDidCancel:(UIImagePickerController )picker;
当用户取消选取时调用;
- (void)imagePickerController:(UIImagePickerController )picker
didFinishPickingMediaWithInfo:(NSDictionary )info;
选取的信息都在info中,info 是一个字典。
字典中的键:
NSString const UIImagePickerControllerMediaType ;指定用户选择的媒体类型(文章最后进行扩展)
NSString const UIImagePickerControllerOriginalImage ;原始
NSString const UIImagePickerControllerEditedImage ;修改后的
NSString const UIImagePickerControllerCropRect ;裁剪尺寸
NSString const UIImagePickerControllerMediaURL ;媒体的URL
NSString const UIImagePickerControllerReferenceURL ;原件的URL
NSString const UIImagePickerControllerMediaMetadata;当来数据来源是照相机的时候这个值才有效
UIImagePickerController 的更多参数参考这里。
代理中的功能参考这里。
UIImagePickerControllerMediaType 包含着KUTTypeImage 和KUTTypeMovie
KUTTypeImage 包含:
const CFStringRef kUTTypeImage ;抽象的类型
const CFStringRef kUTTypeJPEG ;
const CFStringRef kUTTypeJPEG2000 ;
const CFStringRef kUTTypeTIFF ;
const CFStringRef kUTTypePICT ;
const CFStringRef kUTTypeGIF ;
const CFStringRef kUTTypePNG ;
const CFStringRef kUTTypeQuickTimeImage ;
const CFStringRef kUTTypeAppleICNS
const CFStringRef kUTTypeBMP;
const CFStringRef kUTTypeICO;
KUTTypeMovie 包含:
const CFStringRef kUTTypeAudiovisualContent ;抽象的声音视频
const CFStringRef kUTTypeMovie ;抽象的媒体格式(声音和视频)
const CFStringRef kUTTypeVideo ;只有视频没有声音
const CFStringRef kUTTypeAudio ;只有声音没有视频
const CFStringRef kUTTypeQuickTimeMovie ;
const CFStringRef kUTTypeMPEG ;
const CFStringRef kUTTypeMPEG4 ;
const CFStringRef kUTTypeMP3 ;
const CFStringRef kUTTypeMPEG4Audio ;
const CFStringRef kUTTypeAppleProtectedMPEG4Audio;
先存到PILST
NSMutableDictionary info = [[NSMutableDictionary alloc]init];
NSArray paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_%dpng", conut_]]; // 保存文件的名称
[info setObject:filePath forKey:@"img"];
[specialArr addObject:info];
2使用
NSArray paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_%dpng", (int)current]]; // 保存文件的名称
UIImage img = [UIImage imageWithContentsOfFile:filePath];
3用你的方法将 变为壁纸, 不知道怎么用代码直接换壁纸。 你将保存到手机再换吧
UIButton+WebCacheh#import <UIKit/UIKith>
// 为Button添加类别方法
@interface UIButton (WebCache)
- (void)xr_setButtonImageWithUrl:(NSString )urlStr;
@end
UIButton+WebCachem
#import "UIButton+WebCacheh"
@implementation UIButton (WebCache)
- (void)xr_setButtonImageWithUrl:(NSString )urlStr {
NSURL url = [NSURL URLWithString:urlStr];
// 根据的url下载数据
dispatch_queue_t xrQueue = dispatch_queue_create("loadImage", NULL); // 创建GCD线程队列
dispatch_async(xrQueue, ^{
// 异步下载
UIImage img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
// 主线程刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
[self setImage:img forState:UIControlStateNormal];
});
});
}
@end
#import <UIKit/UIKith>
@interface XRViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton ImgBtn;
@end
#import "XRViewControllerh"
#import "UIButton+WebCacheh"
@interface XRViewController ()
@end
@implementation XRViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
}
- (IBAction)loadImg:(id)sender {
[self loadImage];
}
- (void)loadImage {
[_ImgBtn xr_setButtonImageWithUrl:@""];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated
}
@end这个类库提供一个UIImageView类别以支持加载来自网络的远程。具有缓存管理、异步下载、同一个URL下载次数控制和优化等特征。
使用示范的代码:
UITableView使用UIImageView+WebCache类(基本应用,UIImageView的一个category)
前提#import导入UIImageView+WebCacheh文件,然后在tableview的cellForRowAtIndexPath:方法下:
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )
indexPath {
static NSString MyIdentifier = @"MyIdentifier";
UITableViewCell cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cellimageView setImageWithURL:[NSURL URLWithString:@">
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)