我尝试在Swift中使用运动管理器,但我的更新块中的日志从不打印.
var motionManager: CMMotionManager = CMMotionManager()
motionManager.accelerometerUpdateInterval = 0.01
println(motionManager.deviceMotionAvailable) // print true
println(motionManager.deviceMotionActive) // print false
motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler:{
deviceManager, error in
println("Test") // no print
})
println(motionManager.deviceMotionActive) // print false
Run Code Online (Sandbox Code Playgroud)
我的Objective-C实现工作正常.有谁知道为什么我的更新块没有被调用?
我正在尝试使用C++进行实时音高检测.我正在测试一些来自Performous(http://performous.org/)的代码,因为其他一切对我来说都不起作用.我确信这有效,但我不能让它发挥作用.我已经尝试了几个星期了,而且我还没有能够得到任何音调检测代码.
我UIAlertView在我的应用程序中添加了一个抓取用户输入,但我不确定如何添加第三个按钮.理想情况下,三个按钮将水平跨越警报,或者两个按钮位于"取消"按钮上方.下面的代码片段是我用来添加的代码片段UIAlertView.
- (IBAction)initiateSave{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Archive"
message:@"Enter a name to save this as:"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save Session",@"Save",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeDefault;
alertTextField.placeholder = @"eg. My awesome file...";
alert.tag = 1;
[alert show];
[alert release];
self.name = [[alert textFieldAtIndex:0]text];
}
Run Code Online (Sandbox Code Playgroud)

我知道已经有一些问题,但我找不到解决方案.有一个非常简单的代码可以在我的一个项目上正常工作,但不能在另一个项目上工作:
这是UIView的子类的代码.
import UIKit
class test : UIView {
init(frame: CGRect, color: UIColor) {
super.init(frame: frame)
self.backgroundColor = color
self.userInteractionEnabled = true
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
var alert = UIAlertView()
alert.title = "lol"
alert.message = "lol"
alert.addButtonWithTitle("Ok")
alert.show()
}
}
Run Code Online (Sandbox Code Playgroud)
我确信解决方案很简单,但我找不到它.
在NSObject协议中,它定义了一个类似于此的方法:
-(Class) class
Run Code Online (Sandbox Code Playgroud)
什么类型的对象是Class对象?或者它甚至是一个对象?该怎么办?我可以获得基类或采用的协议吗?
大家都知道,因为iOS5有一个原生的Twitter框架,可以很容易地从你的应用发布推文.
有没有办法提示将用户转发到设置应用程序并要求输入用户名和密码的警报?
我知道我可以使用以下代码解决问题:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]];
Run Code Online (Sandbox Code Playgroud)
但那是没有文档的代码..
提前致谢
关心比利(我在SO上的第一篇文章)
我在a UIMenuController中创建了一个自定义UIWebView但它似乎摆脱了之后的"Speak Selection"选项UIMenuController.说话选择选项在所有测试设备的"首选项"中打开,并显示在其他应用程序中,包括非Apple应用程序.是否有可访问性服务或sharedMenuController我可以调用以获取此项目的一部分?
UIMenuItem *copyMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Copy", @"Copy menu item") action:@selector(myappCopy:)];
UIMenuItem *highlightMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Highlight", @"Highlight menu option") action:@selector(myappHighlight:)];
UIMenuItem *unhighlightMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Remove Highlight", @"Remove Highlight menu option")
action:@selector(myappRemoveHighlight:)];
UIMenuItem *noteMenuItem = [[UIMenuItem alloc] initWithTitle:NSLocalizedString(@"Note", @"Note menu options") action:@selector(myappNote:)];
[UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObjects:copyMenuItem, highlightMenuItem, unhighlightMenuItem, noteMenuItem, nil];
[copyMenuItem release];
[highlightMenuItem release];
[unhighlightMenuItem release];
[noteMenuItem release];
Run Code Online (Sandbox Code Playgroud)
我甚至尝试在开始时解析现有的共享菜单项,但我没有看到任何转储在日志中.该方法将在应用启动时调用.
在方法的顶部尝试了这个:
for (UIMenuItem *menuItem in [UIMenuController sharedMenuController].menuItems) {
NSLog(@"title: %@", …Run Code Online (Sandbox Code Playgroud) 我正在编写一个应用程序,其中用户自己拍摄照片,然后通过一系列视图使用导航控制器调整图像.如果用户使用前置摄像头拍摄照片(在支持它的设备上设置为默认设置),则此功能完全正常,但是当我重复此过程时,我会大约一半通过并在发出内存警告后崩溃.
在使用仪器进行分析后,我发现当使用较低分辨率的前置摄像头图像时,我的应用程序内存占用大约保持在20-25 MB,但是当使用后置摄像头时,每次视图更改都会增加33 MB左右,直到它崩溃大约350 MB(在一个4S)
下面是我用来处理将照片保存到文档目录,然后读取该文件位置以将图像设置为a的代码UIImageView.此代码的"读取"部分通过多个视图控制器(viewDidLoad)进行回收,以设置我在每个视图中保存为背景图像的图像.
我删除了所有的图像修改代码,将其剥离到试图隔离问题的熊最小值,我似乎无法找到它.现在,所有应用程序都会在第一个视图中拍摄照片,然后将该照片用作大约10个以上视图的背景图像,并在用户浏览视图堆栈时进行分配.
现在显然分辨率越高的照片会占用更多的内存,但我不明白的是为什么低分辨率的照片似乎没有使用越来越多的内存,而高分辨率的照片不断使用越来越多直到崩溃.
我如何保存和阅读图像:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
jpgData = UIImageJPEGRepresentation(image, 1);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
filePath = [documentsPath stringByAppendingPathComponent:@"image.jpeg"];
[jpgData writeToFile:filePath atomically:YES];
[self dismissModalViewControllerAnimated:YES];
[disableNextButton setEnabled:YES];
jpgData = [NSData dataWithContentsOfFile:filePath];
UIImage *image2 = [UIImage imageWithData:jpgData];
[imageView setImage:image2];
}
Run Code Online (Sandbox Code Playgroud)
现在我知道我可以在保存之前尝试缩放图像,我计划在下一步查看,但我不明白为什么这不起作用.也许我错误地认为ARC在离开堆栈顶部时会自动释放视图及其子视图.
任何人都可以解释为什么我存储我的设备内存?(希望简单的事情,我完全可以忽略)我是不是设法将ARC扔出窗外?
编辑:我如何在其他视图中调用图像
- (void)loadBackground
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString …Run Code Online (Sandbox Code Playgroud) 所以我有一个NSString基本上是一个html包含所有常用html元素的字符串.我想要做的具体事情是从所有img标签中剥离它.
该img标签可能会或可能不会有最大宽度,风格或其他属性,所以我不知道它们的长度达阵.他们总是以/>
我怎么能这样做?
编辑:基于nicolasthenoz答案,我想出了一个需要更少代码的解决方案:
NSString *HTMLTagss = @"<img[^>]*>"; //regex to remove img tag
NSString *stringWithoutImage = [htmlString stringByReplacingOccurrencesOfRegex:HTMLTagss withString:@""];
Run Code Online (Sandbox Code Playgroud) 我很难理解为什么这不起作用:/每次运行项目时,应用程序崩溃都会给我一个'NSInvalidArgumentException',原因:' * - [NSURL initFileURLWithPath:]:nil string parameter'
我遵循了一个教程(我对此很新)并且它对他起作用并且代码完全相同..任何人都可以解释发生了什么?
.h文件
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <QuartzCore/QuartzCore.h>
@interface FirstViewController : UIViewController {
MPMoviePlayerViewController *playerController;
}
-(IBAction)playVideo;
@end
Run Code Online (Sandbox Code Playgroud)
.m文件
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
{
MPMoviePlayerController *mpc;
}
- (IBAction)playButton:(id)sender {
NSString *stringPath = [[NSBundle mainBundle]pathForResource:@"intro" ofType:@"MP4"];
NSURL *url = [NSURL fileURLWithPath:stringPath];
if(url != nil){
mpc = [[MPMoviePlayerController alloc]initWithContentURL:url];
[mpc setMovieSourceType:MPMovieSourceTypeFile];
[[self view]addSubview:mpc.view];
[mpc setFullscreen:YES];
[mpc play];
}
else{
NSLog(@"URL not found");
}
}
@end
Run Code Online (Sandbox Code Playgroud) ios ×8
objective-c ×5
iphone ×3
swift ×2
audio ×1
c++ ×1
class ×1
cocoa-touch ×1
core-motion ×1
fft ×1
html ×1
image ×1
ios5 ×1
ios6 ×1
memory ×1
nsobject ×1
touchesbegan ×1
twitter ×1
types ×1
uialertview ×1
video ×1