小编sol*_*eil的帖子

iOS - 未收到UIEventTypeRemoteControl事件

我在AppDelegate中有这个(didFinishLaunching):

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Run Code Online (Sandbox Code Playgroud)

我尝试在相关的视图控制器中处理事件,但这很不稳定(一些视图控制器会获得事件,而其他视图控制器则不会,即使它们是第一响应者).我尝试了子类化UIApplication.那没用.现在我正在尝试子类化UIWindow并执行此操作(请参阅注释):

- (void)sendEvent:(UIEvent *)event {
if (event.type == UIEventTypeRemoteControl) {
    NSLog(@"I wish this happened"); //this never happens
}
else
{
    NSLog(@"some other event"); //this does happen on any other interaction
    [super sendEvent:event];
}
}

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

if (receivedEvent.type == UIEventTypeRemoteControl) {
    NSLog(@"got remote event"); // this never happens either
    switch (receivedEvent.subtype) {

        case UIEventSubtypeRemoteControlTogglePlayPause:
        {
            NSLog(@"handle play/pause");
            break;
        }


        default:
            break;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我在信息plist中尝试了有和没有这个:

<key>UIBackgroundModes</key>
<array>
        <string>audio</string>
</array>
Run Code Online (Sandbox Code Playgroud)

没有区别.正如Apple文档所述,您可以使用音频控件模拟远程控制事件(双击主页按钮并在底部滚动到它们).当我按下播放或暂停时,它只播放我的iTunes资料库中的音频.我也尝试过Apple耳机上的按钮.没有.

我想要做的就是检测遥控器上的播放/暂停按钮,并处理事件.我还需要做些什么来捕捉这些事件?

events event-handling uiwindow ios

4
推荐指数
1
解决办法
2428
查看次数

UITableView - 在编辑模式下重用单元格

我发现如果你将表格视图设置为编辑模式,在删除一行后滚动表格时,单元格编辑控件(左边的红色减号)处于随机状态(垂直或水平)滚动表格时的单元格.大概是因为我正在重复使用这样的单元格:

cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
Run Code Online (Sandbox Code Playgroud)

如何强制编辑控件为每个单元格处于正确状态?它应始终处于默认的水平状态,除非我点击它以删除单元格.

编辑:这是单元格代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"WhateverIdentifier";
    MyCell *cell = nil;

    //This IF statement fixes the problem, but then I'm not reusing the cells
    if (!tableView.isEditing) {
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    }

    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil] objectAtIndex:0];

    }

    //customize cell

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

uitableview ios

4
推荐指数
1
解决办法
750
查看次数

绘图时将变换应用于UIImage

尝试将可缩放或旋转的较小图像叠加到较大图像上:

+ (UIImage*)addToImage:(UIImage *)baseImage newImage:(UIImage*)newImage atPoint:(CGPoint)point transform:(CGAffineTransform)transform {

    UIGraphicsBeginImageContext(baseImage.size);
    [baseImage drawInRect:CGRectMake(0, 0, baseImage.size.width, baseImage.size.height)];

    [newImage drawAtPoint:point];
    //How would I apply the transform?

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

return result;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我如何将CGAffineTransform应用于newImage?

quartz-graphics uiimage cgaffinetransform ios

4
推荐指数
1
解决办法
8688
查看次数

iOS - UIImagePickerController在设备上崩溃

此代码在模拟器中运行良好,但每次在设备(iPhone 3GS)上崩溃,就在我拍照时.这段代码有问题吗?当我使用分配配置文件时,活动内存在崩溃时仅为3-4 MB,因此应用程序似乎没有内存不足.我正在使用ARC.

-(IBAction)chooseImageNew:(UIButton*)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;

    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentModalViewController:imagePicker animated:YES];
}
else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No Camera Available." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
Run Code Online (Sandbox Code Playgroud)

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *img = [info objectForKey:@"UIImagePickerControllerEditedImage"];
self.userPicture.image = img;
[self.images replaceObjectAtIndex:0 withObject:img];

[self dismissModalViewControllerAnimated:YES];

}
Run Code Online (Sandbox Code Playgroud)

crash uiimagepickercontroller ios

3
推荐指数
1
解决办法
2644
查看次数

Xcode 5中的键绑定

我曾经这样做: Xcode重复行

但是IDETextKeyBindingSet.plist不再存在.

有一个名为"KeyBindings"的文件夹,里面有一个空白文本文件.

那么我们现在如何设置键绑定呢?

具体来说,我只是希望command-D复制光标所在的行,这是除Xcode之外的所有其他IDE的基本功能.

xcode keyboard-shortcuts key-bindings xcode5

3
推荐指数
1
解决办法
3215
查看次数

在CGContextRef中为drawInRect应用变换

在下面的代码中,我将一个小图像添加到一个大图像.我需要能够将变换应用于小图像.现在有些奇怪的事情正在发生,例如,如果我传入一个变换CGAffineTransformMakeRotation(M_PI_2),newImage不会旋转到位,而是在屏幕外的某处绘制.我怎样才能解决这个问题?

+ (UIImage*)addToImage:(UIImage *)baseImage newImage:(UIImage*)newImage atPoint:(CGPoint)point transform:(CGAffineTransform)transform {

    UIGraphicsBeginImageContextWithOptions(baseImage.size, NO, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();

    [baseImage drawInRect:CGRectMake(0, 0, baseImage.size.width, baseImage.size.height)];

    CGRect newRect = CGRectMake(point.x,
                                     point.y,
                                     newImage.size.width,
                                     newImage.size.height);

    CGContextConcatCTM(context, transform);
    [newImage drawInRect:newRect];

    UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}
Run Code Online (Sandbox Code Playgroud)

请注意,我只想将变换应用于newImage,而不是baseImage.

quartz-graphics uiimage cgcontext cgaffinetransform ios

3
推荐指数
1
解决办法
5091
查看次数

替换 NSMutableAttributedString 中的字符

这适用于常规NSString

NSString *newString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];
Run Code Online (Sandbox Code Playgroud)

但是没有这样的方法NSMutableAttributedString。如何删除 中逗号的所有实例NSMutableAttributedString

nsstring ios nsmutableattributedstring

3
推荐指数
1
解决办法
5055
查看次数

Swift中的字典数组

似乎找不到合适的语法.试图从json文件中获取一系列dicts:

let books:Array = jsonDict["books"] as Array
//Cannot convert the expression's type Array<$T4> to type StringLiteralConvertible

let books:Array = jsonDict["books"] as Array<Dictionary>
//Reference to generic type "Dicionary" requires arguments in <...>
Run Code Online (Sandbox Code Playgroud)

json看起来像这样:

{
  "id": "1",
  "title": "title one",
  "books": [
    {
      "id": "1",
      "title": "book title one"
    },
    {
      "id": "2",
      "title": "book title two"
    }
    ]
}
Run Code Online (Sandbox Code Playgroud)

arrays json dictionary swift

2
推荐指数
1
解决办法
3094
查看次数

iPhone 6 - 应用程序无法填满屏幕

我的大多数应用程序在iPhone 6上运行时会扩展,但出于某种原因,我的某个应用程序看起来像这样:

在此输入图像描述

什么会导致它不仅仅是放大并填满屏幕?

编辑:这是一些repro步骤:

创建一个新项目(单视图).关闭尺寸类别,只需使用iPhone.

使背景变成橙色.

在(20,20,280,200)添加黄色UIView子视图.

在Xcode中你得到这个:

在此输入图像描述

现在在iPhone 6上模拟.你得到这个:

在此输入图像描述

为什么不扩大规模并且看起来正常?

xcode uiview ios autolayout iphone-6

2
推荐指数
1
解决办法
3509
查看次数

snapshotViewAfterScreenUpdates创建空白图像

我正在尝试使用以下代码创建一些复合UIImage对象:

someImageView.image = [ImageMaker coolImage];
Run Code Online (Sandbox Code Playgroud)

ImageMaker:

- (UIImage*)coolImage {
    UIView *composite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coolImage"]]; //This is a valid image - can be viewed when debugger stops here
    [composite addSubview:imgView];

    UIView *snapshotView = [composite snapshotViewAfterScreenUpdates:YES];
//at this point snapshotView is just a blank image
    UIImage *img = [self imageFromView:snapshotView];
    return img;

}

- (UIImage *)imageFromView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return …
Run Code Online (Sandbox Code Playgroud)

uiview uiimageview uiimage ios

2
推荐指数
1
解决办法
8500
查看次数