使用相机胶卷图像设置背景
Posted
技术标签:
【中文标题】使用相机胶卷图像设置背景【英文标题】:Set Background using camera roll images 【发布时间】:2011-06-06 21:45:31 【问题描述】:我想知道,如何使用相机胶卷图像更改我的图像应用背景。有任何想法吗???如何获得相机滚动路径或类似的东西!!!
谢谢!!!!
【问题讨论】:
【参考方案1】:以下代码使用工具栏上的按钮,按下时会调出相机胶卷,然后您就可以选择要设置的图像
首先添加 MobileCoreServices 框架
在.h文件中添加以下代码
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
@interface cameraViewController : UIViewController
<UIImagePickerControllerDelegate,
UINavigationControllerDelegate, UIPopoverControllerDelegate>
@property (nonatomic) BOOL newMedia;
@property (nonatomic, strong) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) UIPopoverController *popoverController;
@property (nonatomic, strong) IBOutlet UIToolbar *toolbar;
- (IBAction)useCameraRoll: (id)sender;
@end
在.m文件中添加如下代码:
@synthesize imageView, popoverController, toolbar, newMedia;
- (IBAction) useCameraRoll: (id)sender
if ([self.popoverController isPopoverVisible])
[self.popoverController dismissPopoverAnimated:YES];
else
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum])
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
self.popoverController.delegate = self;
[self.popoverController
presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
newMedia = NO;
#pragma mark -
#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
[self.popoverController dismissPopoverAnimated:true];
NSString *mediaType = [info
objectForKey:UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:YES];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
UIImage *image = [info
objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image;
if (newMedia)
UIImageWriteToSavedPhotosAlbum(image,
self,
@selector(image:finishedSavingWithError:contextInfo:),
nil);
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
// Code here to support video if enabled
-(void)image:(UIImage *)image
finishedSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo
if (error)
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Save failed"
message: @"Failed to save image"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
[self dismissModalViewControllerAnimated:YES];
在 viewDidUnload 中添加以下内容:
- (void)viewDidUnload
self.imageView = nil;
self.popoverController = nil;
self.toolbar = nil;
不要忘记将 imageview 连接到您的图像,并将 cameraroll 按钮连接到您的按钮。
【讨论】:
【参考方案2】:查看资产库的文档。
【讨论】:
【参考方案3】:-(void) uourCallingMethod UIImage *img = [UIImage imageNamed: [NSString stringWithFormat:@"image.jpg", i]]; UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
(void) image:(UIImage *)image didFinishSavingWithError:(NSError *) error contextInfo:(void *) contextInfo【讨论】:
以上是关于使用相机胶卷图像设置背景的主要内容,如果未能解决你的问题,请参考以下文章
保存用相机拍摄或从相机胶卷中选择的图像时出现长时间延迟 - iPhone